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];
}
}
In the edited code, trying to "zoom out a bit more" than the calculated MKMapRect
doesn't work because:
MKCoordinateRegionForMapRect
, it is passing annotationMapRect
(the rect for a single annotation) instead of showMapRect
(the rect for all the annotations).showMapRect
and expanding it a bit (my multiplying it by say 1.1).setRegion
with the "zoomed out region", code again calls setVisibileMapRect
with the original showMapRect
which undoes all the work with the zoomed out region.You could zoom out a bit using setRegion
after fixing the above but the simpler way as you already found out is to use setVisibleMapRect:edgePadding:animated:
instead of just setVisibleMapRect:
.
Regarding the problem with mapViewDidFailLoadingMap
:
It's hard to say why the map is failing to load in your specific case.
But whatever the reason(s) might be, if you must let the user know that the map failed to load, my suggestion would be to use a UILabel
instead of a UIAlertView
.
Rather than make the user tap OK every time the map view has a problem loading, show/hide the label when the map view fails/finishes loading.
This way, the user is "alerted" but they aren't annoyed having to constantly tap OK.
This might give the user a more pleasing experience.
Make the label a subview of the same view that the map view is a subview of. That is, make them siblings. Don't make the label a subview of the map view. Set the label initially to "hidden" or with alpha
set to 0 so you could animate the label show/hide in the delegate methods. You can put the label in front of the map view (if there's no room on the screen) but not a subview of the map view.
Example:
//initialize label alpha to 0 in viewDidLoad...
mapLoadFailedLabel.alpha = 0;
-(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
{
[UIView animateWithDuration:0.5 animations:^{
mapLoadFailedLabel.alpha = 1.0;
}];
}
-(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
{
[UIView animateWithDuration:0.5 animations:^{
mapLoadFailedLabel.alpha = 0.0;
}];
}