我節省地圖區域爲用戶默認時,我的iPhone應用程序被關閉這樣的:的MKMapView秀正確保存區域
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"];
當應用程序再次啓動,Ш讀取這些值回以同樣的方式,使用戶可以看到完全相同的地圖視圖,因爲它是最後一次:
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]);
我從用戶默認值讀出的區域是(不奇怪)完全相同的,當它被保存爲。請注意,保存的內容直接來自地圖,因此不會以任何方式進行轉換。我用setRegion:
方法將其重新設置在地圖上,但是它不同!
示例結果:
Region read : 50.241110 8.891555 0.035683 0.042915<br>
Region on map: 50.241057 8.891544 0.050499 0.054932
是否有人知道爲什麼會這樣?
我發現了一個解決方案,可以準確獲得您想要的地圖區域:請參閱我的帖子http://stackoverflow.com/questions/3612007/mkmapview-setregion-snaps-to-predefined-zoom-levels/7935 – AlexWien 2011-10-29 13:06:54