我有一个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];}解决方法 不幸的是,这是 Google Maps API的限制,它只在设置地图的缩放级别时接受整数值:当你设置MKMapVIEw的显示区域时,Apple的MapKit代码调用底层的Google Maps API,结果 – 无论你使用哪种MapKit方法设置区域 – 是一个缩小到最接近的整数缩放级别的地图.
Troy Brant的代码将带您完整的循环,并在MapKit API上方放置一个图层,允许您直接设置缩放级别…但最终您无法精确控制MKMapVIEw显示的区域,除非您想要的缩放级别map恰好是一个整数.
Stack Overflow上出现了这个问题的几个变种(例如,MKMapView setRegion “snaps” to predefined zoom levels?和MKMapView show incorrectly saved region),但到目前为止还没有人想出一种编程方式来制作具有非整数缩放级别的地图,我怀疑它需要合作谷歌与苹果之间的关系永远让它成真.
总结以上是内存溢出为你收集整理的iphone – 将MapKit地图的缩放/边界与RouteMe地图的缩放/边界相匹配全部内容,希望文章能够帮你解决iphone – 将MapKit地图的缩放/边界与RouteMe地图的缩放/边界相匹配所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)