我正在使用MKMapView,并在大约5至10公里的区域中向地图添加了许多注释图钉。当我运行该应用程序时,地图开始缩小以显示整个世界,什么是最好的缩放地图方式以使图钉适合视图?
编辑: 我最初的想法是使用MKCoordinateRegionMake并从我的注释中计算坐标中心,longitudeDelta和latitudeDelta。我很确定这是可行的,但是我只是想检查一下我是否没有遗漏任何明显的东西。
添加的代码,顺便说一句:FGLocation是符合的类MKAnnotation
,locationFake是NSMutableArray
这些对象之一。总是欢迎评论....
- (MKCoordinateRegion)regionFromLocations {
CLLocationCoordinate2D upper = [[locationFake objectAtIndex:0] coordinate];
CLLocationCoordinate2D lower = [[locationFake objectAtIndex:0] coordinate];
// FIND LIMITS
for(FGLocation *eachLocation in locationFake) {
if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude;
if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude;
}
// FIND REGION
MKCoordinateSpan locationSpan;
locationSpan.latitudeDelta = upper.latitude - lower.latitude;
locationSpan.longitudeDelta = upper.longitude - lower.longitude;
CLLocationCoordinate2D locationCenter;
locationCenter.latitude = (upper.latitude + lower.latitude) / 2;
locationCenter.longitude = (upper.longitude + lower.longitude) / 2;
MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);
return region;
}