将MapKit贴图的缩放/边界与RouteMe贴图的缩放/边界匹配

And*_*son 5 iphone mapkit ipad ios route-me

编辑:我相信我的问题是这个代码适用于整数缩放级别,但我希望它适用于浮动缩放级别.

我有一个iOS应用程序,用户可以在基于RouteMe的地图和基于MapKit的地图之间切换.

当他们切换源时,我希望能够在一个中显示完全相同的区域.但是,我无法弄清楚如何使它们匹配,因为RouteMe和MapKit使用不同的数据结构来描述地图边界.

这里有一些代码可以让它有点接近,但并不准确.此代码来自:http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

我不确定这段代码是否应该修复,或者我可能忽略了一个更简单的解决方案.代码从列出的最后一个方法开始执行:

#define MERCATOR_OFFSET 268435456
#define MERCATOR_RADIUS 85445659.44705395

#pragma mark -
#pragma mark Map conversion methods

- (double)longitudeToPixelSpaceX:(double)longitude {
  return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI / 180.0);
}

- (double)latitudeToPixelSpaceY:(double)latitude {
  return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0);
}

- (double)pixelSpaceXToLongitude:(double)pixelX {
  return ((round(pixelX) - MERCATOR_OFFSET) / MERCATOR_RADIUS) * 180.0 / M_PI;
}

- (double)pixelSpaceYToLatitude:(double)pixelY {
  return (M_PI / 2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET) / MERCATOR_RADIUS))) * 180.0 / M_PI;
}


- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView
                             centerCoordinate:(CLLocationCoordinate2D)centerCoordinate
                                 andZoomLevel:(NSInteger)zoomLevel {
  // convert center coordiate to pixel space
  double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude];
  double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude];

  // determine the scale value from the zoom level
  NSInteger zoomExponent = 20 - zoomLevel;
  double zoomScale = pow(2, zoomExponent);

  // scale the map’s size in pixel space
  CGSize mapSizeInPixels = mapView.bounds.size;
  double scaledMapWidth = mapSizeInPixels.width * zoomScale;
  double scaledMapHeight = mapSizeInPixels.height * zoomScale;

  // figure out the position of the top-left pixel
  double topLeftPixelX = centerPixelX - (scaledMapWidth / 2);
  double topLeftPixelY = centerPixelY - (scaledMapHeight / 2);

  // find delta between left and right longitudes
  CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX];
  CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth];
  CLLocationDegrees longitudeDelta = maxLng - minLng;

  // find delta between top and bottom latitudes
  CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY];
  CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight];
  CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat);

  // create and return the lat/lng span
  MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
  return span;
}


- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                    zoomLevel:(NSUInteger)zoomLevel
                     animated:(BOOL)animated {

  // use the zoom level to compute the region
  MKCoordinateSpan span = [self coordinateSpanWithMapView:self       
                               centerCoordinate:centerCoordinate
                                   andZoomLevel:zoomLevel];
  MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);

  // set the region like normal
  [self setRegion:region animated:animated];
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*bes 4

不幸的是,这是Google Maps API的限制,它在设置地图的缩放级别时仅接受整数值:当您设置 MKMapView 的显示区域时,Apple 的 MapKit 代码正在调用底层 Google Maps API,无论您使用哪种 MapKit 方法来设置区域 \xe2\x80\x93,结果 \xe2\x80\x93 都是缩小到最接近的整数缩放级别的地图。

\n\n

Troy Brant 的代码带你绕了一圈,并在 MapKit API 之上放置了一个层,允许你直接设置缩放级别\xe2\x80\xa6,但最终你无法精确控制由MKMapView,除非您所需的地图的缩放级别恰好是整数。

\n\n

Stack Overflow 上出现了关于这个问题的几种变体(例如,MKMapView setRegion“捕捉”到预定义的缩放级别?MKMapView 显示错误保存的区域),但到目前为止,没有人想出一种编程方法来制作带有非-整数缩放级别,我怀疑谷歌和苹果需要合作才能实现这一目标。

\n