I am trying to plot multiple points on MKMapView. Between two straight points, it is easy -- I just find the middle coordinates and use them:
// Center
CLLocationCoordinate2D center;
center.latitude = self.midLatitude;
center.longitude = self.midLongitude;
myRegion.center = center;
But what if I have Los Angeles, Washington DC, and Seattle on the map. I have my own code which calculates the center based on the first and the last coordinates in the array. In the above case, I only see LA and Seattle.
Is there another way to determine the center between three points?
Thank you!
EDIT:
New code with MKMapRect added:
-(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];
}
}