ios - MKMapView 显示错误保存的区域

标签 ios objective-c mkmapview nsuserdefaults mkcoordinateregion

当我的 iPhone 应用程序像这样关闭时,我将 map 区域保存到用户默认值中:

MKCoordinateRegion region = mapView.region;
[[NSUserDefaults standardUserDefaults] setDouble:region.center.latitude forKey:@"map.location.center.latitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.center.longitude forKey:@"map.location.center.longitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.latitudeDelta forKey:@"map.location.span.latitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.longitudeDelta forKey:@"map.location.span.longitude"];

当应用程序再次启动时,Ш 以相同的方式读回这些值,以便用户可以看到与上次完全相同的 map View :

MKCoordinateRegion region;

region.center.latitude  = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.center.latitude"];
region.center.longitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.center.longitude"];
region.span.latitudeDelta  = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.latitude"];
region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.longitude"];

NSLog([NSString stringWithFormat:@"Region read  : %f %f %f %f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta]);

[mapView setRegion:region];

NSLog([NSString stringWithFormat:@"Region on map: %f %f %f %f", mapView.region.center.latitude, mapView.region.center.longitude, mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta]);

我从用户默认值中读取的区域(毫不奇怪)与保存时完全相同。请注意,保存的内容直接来自 map ,因此不会以任何方式进行转换。我使用 setRegion: 方法将其重新设置到 map 上,但随后就不一样了!

示例结果:

Region read  : 50.241110 8.891555 0.035683 0.042915<br>
Region on map: 50.241057 8.891544 0.050499 0.054932

有人知道为什么会这样吗?

最佳答案

这里的问题是,当您设置区域时, map 缩放级别会“跳出”到最近的缩放阈值。 (我怀疑这些缩放阈值是您双击或双指点击时获得的缩放量)

因此,例如,如果 map 显示缩放级别 1,并且您将区域设置为相同的跨度值:region = [mapView region]; [mapView setRegion:region]; 它将“捕捉”到级别 1 上方最近的缩放级别,即级别 2,您将缩小大约两倍。 p>

原始海报的解决方法是在设置区域之前稍微减小跨度值,这样当 View 弹出时,它会弹出到它所在的缩放级别,而不是上面的缩放级别。

例如

region.span.latitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.latitude"] * 0.999;

region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.longitude"] * 0.999;

如果用户一直在通过双击进行缩放(并因此从一个阈值跳到另一个阈值),这会非常有效,几乎可以完全将他们返回到相同的 View 。

然而,如果他们捏缩放并且 View 在缩放阈值之间的中间,它仍然会突然跳到下一个级别。在那种情况下不太好,但目前还没有解决办法。

Apple 雷达上有这方面的公开错误,希望它会在未来的版本中得到修复。

关于ios - MKMapView 显示错误保存的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1383296/

相关文章:

ios - UICollectionView 委托(delegate)中的搜索栏 subview

ios - "CustomAnnotation is tapped"在MKMapView中的使用

ios - UICollectionView 不渲染单元格 - 没有代码只有 Storyboard

iphone - 如何刷新 iPhone 的 UIWebview?

ios - Xcode 构建 Google Analytics SDK 失败

iphone - 对 <TSActivityMapVC : 0x81b1000> 的开始/结束外观转换的调用不平衡

ios - popoverContentSize 在 iOS 8 中没有响应

iphone - MKPinAnnotationView 与 MKAnnotationView

ios - 在 iOS 上绘制 session 中心房间 map

objective-c - 滑动 UITableViewCell