我有一个数组allCollections,它包含用户通过我的iOS应用程序记录的以编程方式创建的CLLocations数组.allCollections中的每个子数组都包含所有行程中的所有位置点.
我从allCollections中的数组中的CLLocations中绘制MKPolylines来表示MKMapView上的那些行程.我的问题是:将折线添加到地图中,我将如何以编程方式缩放和居中地图以显示所有这些?
当我的iPhone应用程序关闭时,我将地图区域保存为用户默认值:
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"];
Run Code Online (Sandbox Code Playgroud)
当应用程序再次启动时,Ш以相同的方式读取这些值,以便用户可以看到与上次完全相同的地图视图:
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]);
Run Code Online (Sandbox Code Playgroud)
我从用户默认值中读取的区域(毫不奇怪)与保存时完全相同.请注意,保存的内容直接来自地图,因此不会以任何方式进行转换.我用setRegion:方法将它设置在地图上,但后来却与众不同!
示例结果:
Region read : 50.241110 8.891555 0.035683 …Run Code Online (Sandbox Code Playgroud)