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

时间:2011-11-09 01:22:40

标签: iphone ios ipad mapkit 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];
}

1 个答案:

答案 0 :(得分:4)

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

Troy Brant的代码将带您完整的循环,并在MapKit API上方放置一个图层,允许您直接设置缩放级别...但最终您无法精确控制MKMapView显示的区域,除非缩放级别你想要的地图恰好是一个整数。

这个问题的几个变体出现在Stack Overflow上(例如MKMapView setRegion "snaps" to predefined zoom levels?MKMapView show incorrectly saved region),但到目前为止还没有人想出一种编程方法来制作一个非整数的地图缩放级别,我怀疑谷歌和苹果之间的合作是否会让它成为现实。

相关问题