iphone - 将 MapKit map 的缩放/边界与 RouteMe map 的缩放/边界匹配

标签 iphone ios ipad mapkit route-me

编辑:我认为我的问题是此代码适用于整数缩放级别,但我希望它适用于 float 缩放级别。

我有一个 iOS 应用程序,用户可以在其中切换基于 RouteMe 的 map 和基于 MapKit 的 map 。

当他们切换来源时,我希望能够在一个中显示与另一个中完全相同的区域。但是,我不知道如何使它们匹配,因为 RouteMe 和 MapKit 使用不同的数据结构来描述 map 边界。

这里有一些代码使它有点接近,但并不精确。此代码来自: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];
}

最佳答案

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

Troy Brant 的代码带你转了一圈,并在 MapKit API 之上放置了一个层,允许你直接设置缩放级别......但最终你无法精确控制 MKMapView 显示的区域,除非缩放级别你想要的 map 恰好是一个整数。

Stack Overflow 上出现了关于此问题的多种变体(例如 MKMapView setRegion "snaps" to predefined zoom levels?MKMapView show incorrectly saved region),但到目前为止还没有人想出一种程序化的方法来制作具有非整数缩放级别的 map ,并且我怀疑这需要 Google 和 Apple 之间的合作才能实现。

关于iphone - 将 MapKit map 的缩放/边界与 RouteMe map 的缩放/边界匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059321/

相关文章:

iphone - 哪个谷歌地图版本适用于 ios 5

ios - UICollectionview 用户交互禁用第一个单元格,也禁用滚动上的其他几个单元格

ios - 从 Collection View 传递到 View Controller 的简单图像数据失败

iphone - NSLocalizedString : Why is genstrings adding 1$ and 2$ to my format specifiers?

iphone - 如何更新 UITableView 的屏幕外单元格

ios - UIBlurEffect 在设备上不模糊但在模拟器中有效

iphone - 如何仅使用地址簿中的电子邮件地址访问联系人?

ios - 如何检查lib(静态或动态)库是为iOS模拟器或Mac OSX构建的

iphone - 从 iPhone 应用程序打开家长锁

iphone - 如何识别来自iphone的用户点击 "home screen"