×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: Objective-C
Posted by: Davide
Added: Oct 15, 2011 11:07 AM
Views: 134
  1. // pad our map by 10% around the farthest annotations
  2. #define MAP_PADDING 1.1
  3.  
  4. // we'll make sure that our minimum vertical span is about a kilometer
  5. // there are ~111km to a degree of latitude. regionThatFits will take care of
  6. // longitude, which is more complicated, anyway.
  7. #define MINIMUM_VISIBLE_LATITUDE 0.01
  8.  
  9. - (void)mapZoom
  10. {
  11.     CLLocationDegrees minLatitude = 90.0;
  12.     CLLocationDegrees maxLatitude = -90.0;
  13.     CLLocationDegrees minLongitude = 180.0;
  14.     CLLocationDegrees maxLongitude = -180.0;
  15.    
  16.     for (MapPoint *p in [self.mapView annotations]) {
  17.         if (p.coordinate.latitude < minLatitude)
  18.             minLatitude = p.coordinate.latitude;
  19.         if (p.coordinate.latitude > maxLatitude)
  20.             maxLatitude = p.coordinate.latitude;
  21.         if (p.coordinate.longitude < minLongitude)
  22.             minLongitude = p.coordinate.longitude;
  23.         if (p.coordinate.longitude > maxLongitude)
  24.             maxLongitude = p.coordinate.longitude;
  25.     }
  26.    
  27.     MKCoordinateRegion region;
  28.     region.center.latitude = (minLatitude + maxLatitude) / 2;
  29.     region.center.longitude = (minLongitude + maxLongitude) / 2;
  30.    
  31.     region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;
  32.    
  33.     region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE) ? MINIMUM_VISIBLE_LATITUDE : region.span.latitudeDelta;
  34.    
  35.     region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;
  36.    
  37.     MKCoordinateRegion scaledRegion = [self.mapView regionThatFits:region];
  38.     [self.mapView setRegion:scaledRegion animated:YES];
  39. }