Maison > Questions et réponses > le corps du texte
场景:现在有很多骑行记录软件,打开某次记录,会把骑行的路径添加到地图上,路径全部显示在屏幕中并且缩放得刚刚好
问题:在已知多个坐标点的情况下,如何获取这些坐标点全部显示在屏幕上对应地图的中心坐标和合适的缩放比例
想法:是否有一个方法把这些坐标做为参数传进去就可以得到我想要的那两个值呢?
PHP中文网2017-04-17 15:28:47
可以参考
如下代码:
- (void)zoomToMapPoints:(MKMapView*)mapView annotations:(NSArray*)annotations
{
double minLat = 360.0f, maxLat = -360.0f;
double minLon = 360.0f, maxLon = -360.0f;
for (MKPointAnnotation *annotation in annotations) {
if ( annotation.coordinate.latitude < minLat ) minLat = annotation.coordinate.latitude;
if ( annotation.coordinate.latitude > maxLat ) maxLat = annotation.coordinate.latitude;
if ( annotation.coordinate.longitude < minLon ) minLon = annotation.coordinate.longitude;
if ( annotation.coordinate.longitude > maxLon ) maxLon = annotation.coordinate.longitude;
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) / 2.0, (minLon + maxLon) / 2.0);
MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
}