Guest User

Untitled

a guest
May 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. - (void)centerMapAroundAnnotations:(MKMapView *)mapView {
  2. if ([[mapView annotations] count] == 0) return;
  3.  
  4. CLLocationCoordinate2D min;
  5. CLLocationCoordinate2D max;
  6.  
  7. BOOL first = YES;
  8.  
  9. NSUInteger numberOfValidAnnotations = 0;
  10.  
  11. for (id <MKAnnotation> annotation in [mapView annotations]) {
  12. if ([annotation isKindOfClass:[AVAnnotation class]]) {
  13. if (first) {
  14. min = annotation.coordinate;
  15. max = annotation.coordinate;
  16. first = NO;
  17. } else {
  18. min.latitude = MIN(min.latitude, annotation.coordinate.latitude);
  19. min.longitude = MIN(min.longitude, annotation.coordinate.longitude);
  20.  
  21. max.latitude = MAX(max.latitude, annotation.coordinate.latitude);
  22. max.longitude = MAX(max.longitude, annotation.coordinate.longitude);
  23. }
  24.  
  25. ++numberOfValidAnnotations;
  26. }
  27. }
  28.  
  29. if (numberOfValidAnnotations == 0) return;
  30.  
  31. CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude: min.latitude longitude: min.longitude];
  32. CLLocation* locSouthEast = [[CLLocation alloc] initWithLatitude: min.latitude longitude: max.longitude];
  33. CLLocation* locNorthEast = [[CLLocation alloc] initWithLatitude: max.latitude longitude: max.longitude];
  34.  
  35. CLLocationCoordinate2D regionCenter;
  36.  
  37. regionCenter.latitude = (min.latitude + max.latitude) / 2.0;
  38. regionCenter.longitude = (min.longitude + max.longitude) / 2.0;
  39.  
  40. CLLocationDistance latMeters = [locSouthEast getDistanceFrom:locNorthEast];
  41. CLLocationDistance lonMeters = [locSouthEast getDistanceFrom:locSouthWest];
  42.  
  43. MKCoordinateRegion region;
  44.  
  45. region = MKCoordinateRegionMakeWithDistance(regionCenter, latMeters, lonMeters);
  46.  
  47. MKCoordinateRegion fitRegion = [mapView regionThatFits:region];
  48. [mapView setRegion:fitRegion animated:YES];
  49.  
  50. [locSouthWest release];
  51. [locSouthEast release];
  52. [locNorthEast release];
  53. }
Add Comment
Please, Sign In to add comment