我正在尝试在 MKMapView 上绘制多个点。在两个直线之间,这很容易——我只需找到中间坐标并使用它们:
// Center
CLLocationCoordinate2D center;
center.latitude = self.midLatitude;
center.longitude = self.midLongitude;
myRegion.center = center;
但是,如果地图上有洛杉矶、华盛顿特区和西雅图呢?我有自己的代码,它根据数组中的第一个和最后一个坐标计算中心。在上述情况下,我只看到洛杉矶和西雅图。
有没有另一种方法来确定三点之间的中心?
谢谢!
编辑:
添加了 MKMapRect 的新代码:
-(void) addAnnotation {
MKMapRect showMapRect = MKMapRectNull;
CLLocationCoordinate2D mapLocation;
IGAMapAnnotation *mapAnnotation;
// Calculate how many points are included in the plan
NSInteger numberOfPoints = [coordinatesTempArray count];
MKMapPoint annotationMapPoint;
MKMapRect annotationMapRect;
if (numberOfPoints > 0) {
// Trying to add coordinates from the array of coordinates
for (NSInteger i=0; i < ([coordinatesTempArray count]); i++) {
... // Code that adds annotations
// Adding the annotation to the array that will be added to the map
[mapLocations addObject:mapAnnotation];
// Adding Annotation coordinates to MKMapRect so that they all be visible in the view
annotationMapPoint = MKMapPointForCoordinate(mapLocation);
annotationMapRect = MKMapRectMake(annotationMapPoint.x, annotationMapPoint.y, 0, 0);
showMapRect = MKMapRectUnion(showMapRect, annotationMapRect);
}
}
// Showing all annotations at a time
self.mapView.visibleMapRect = showMapRect;
// Now I am trying to zoom out a bit since the extreme annotation are right at the border of the mapview. THIS DOES NOT WORK.
MKCoordinateRegion region;
region = MKCoordinateRegionForMapRect(annotationMapRect);
MKCoordinateSpan span;
span.latitudeDelta = 0.09;
span.longitudeDelta = 0.09;
region.span = span;
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
// Showing all annotations at a time
self.mapView.visibleMapRect = showMapRect;
// Adding annotations to the map
[self.mapView addAnnotations:mapLocations];
}
}