I thought I would share a handy little piece of code that enables you to zoom the MKMapView control on the iPhone to contain all the annotations that it holds. This should hopefully cut down on your users having to pinch-zoom and scroll to find annotations scattered throughout the map!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | -(void)zoomToFitMapAnnotations:(MKMapView*)mapView { if([mapView.annotations count] == 0) return; CLLocationCoordinate2D topLeftCoord; topLeftCoord.latitude = -90; topLeftCoord.longitude = 180; CLLocationCoordinate2D bottomRightCoord; bottomRightCoord.latitude = 90; bottomRightCoord.longitude = -180; for(MapAnnotation* annotation in mapView.annotations) { topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); } MKCoordinateRegion region; region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides region = [mapView regionThatFits:region]; [mapView setRegion:region animated:YES]; } |
16 Responses to “Zoom MKMapView To Fit Annotations”
Dev November 28, 2009
Thanks for this tip!
|Toshiro December 8, 2009
Thanks!!
|TheDesignZoo February 7, 2010
Brilliant. Thanks for posting stuff like this! Timesaver!
|zep February 7, 2010
Nice work !
|Thank’s a lot ! really much time saved !
THDesign February 12, 2010
Awesome!
|That’s exactly what I was looking for, has saved me a bunch of time. Many thanks.
Dev February 20, 2010
As stated above, this is awesome! Thanks for the insight on this.
|cagreen March 19, 2010
Perfect! Exactly what I was looking for. Thanks for taking the time to post this.
|bob doe April 2, 2010
Saved my life.
|pascal April 17, 2010
please where’s the class MapAnnotation
|Mario Rosales April 23, 2010
Awesome!!, works great. Thank you very much
|Sudakura April 30, 2010
Great work. Thank you very much
|Felipe June 30, 2010
Man, this code IS indeed good! Thanks for sharing!
|Vijay Kumar Soni July 14, 2010
Excellent…. thank you very very very much
|Nickdep July 20, 2010
Presto Magic!!!
|gary July 30, 2010
Hmm,
I must be doing something wrong. I have dropped this routine into my view controller and I have an MKMapView called mapView. When I compile I get ‘Local Declaration of ‘mapView’ hides instance variable’ and also ‘MapAnnotation is undeclared’
Any ideas?
Many Thanks
Gary
|