Iphone 将MapKit地图的缩放/边界与RouteMe地图的缩放/边界匹配
编辑:我认为我的问题是,这段代码适用于整数缩放级别,但我希望它适用于浮动缩放级别。 我有一个iOS应用程序,用户可以在基于RouteMe的地图和基于MapKit的地图之间切换 当他们切换源时,我希望能够在一个和另一个中显示完全相同的区域。但是,我不知道如何使它们匹配,因为RouteMe和MapKit使用不同的数据结构来描述地图边界 下面是一些代码,使它有点接近,但它不是准确的。此代码来自: 我不确定这段代码是否应该被修复,或者我可能忽略了一个更简单的解决方案。代码从列出的最后一个方法开始执行:Iphone 将MapKit地图的缩放/边界与RouteMe地图的缩放/边界匹配,iphone,ios,ipad,mapkit,route-me,Iphone,Ios,Ipad,Mapkit,Route Me,编辑:我认为我的问题是,这段代码适用于整数缩放级别,但我希望它适用于浮动缩放级别。 我有一个iOS应用程序,用户可以在基于RouteMe的地图和基于MapKit的地图之间切换 当他们切换源时,我希望能够在一个和另一个中显示完全相同的区域。但是,我不知道如何使它们匹配,因为RouteMe和MapKit使用不同的数据结构来描述地图边界 下面是一些代码,使它有点接近,但它不是准确的。此代码来自: 我不确定这段代码是否应该被修复,或者我可能忽略了一个更简单的解决方案。代码从列出的最后一个方法开始执行:
#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];
}
不幸的是,这是的一个限制,它在设置地图的缩放级别时只接受整数值:当您设置MKMapView的显示区域时,Apple的MapKit代码正在调用底层的Google Maps API,结果——无论使用哪种MapKit方法设置面积——都是一张缩小到最接近的整数缩放级别的地图 特洛伊·布兰特(Troy Brant)的代码带您走了一整圈,并在MapKit API上方放置了一层,允许您直接设置缩放级别……但最终您无法精确控制MKMapView显示的区域,除非所需地图的缩放级别恰好是一个整数
在堆栈溢出(例如和)上出现了关于这个问题的几种变体,但到目前为止,还没有人提出一种编程方法来制作具有非整数缩放级别的贴图,我怀疑这需要谷歌和苹果的合作才能实现。我注意到,关于MKMapView,有一点是,如果你设置了区域,然后立即读取,你会得到不同的值。在我们的应用程序中,我设置触发委托回调的值,然后读取新值并使用它来确定将覆盖放置在何处。不确定这是否有帮助-(这个问题总结得很好,缺乏解决方案。我想我还得想些别的办法,因为这目前不可能。谢谢Scott。