Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)centerMapAroundAnnotations:(MKMapView *)mapView {
- if ([[mapView annotations] count] == 0) return;
- CLLocationCoordinate2D min;
- CLLocationCoordinate2D max;
- BOOL first = YES;
- NSUInteger numberOfValidAnnotations = 0;
- for (id <MKAnnotation> annotation in [mapView annotations]) {
- if ([annotation isKindOfClass:[AVAnnotation class]]) {
- if (first) {
- min = annotation.coordinate;
- max = annotation.coordinate;
- first = NO;
- } else {
- min.latitude = MIN(min.latitude, annotation.coordinate.latitude);
- min.longitude = MIN(min.longitude, annotation.coordinate.longitude);
- max.latitude = MAX(max.latitude, annotation.coordinate.latitude);
- max.longitude = MAX(max.longitude, annotation.coordinate.longitude);
- }
- ++numberOfValidAnnotations;
- }
- }
- if (numberOfValidAnnotations == 0) return;
- CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude: min.latitude longitude: min.longitude];
- CLLocation* locSouthEast = [[CLLocation alloc] initWithLatitude: min.latitude longitude: max.longitude];
- CLLocation* locNorthEast = [[CLLocation alloc] initWithLatitude: max.latitude longitude: max.longitude];
- CLLocationCoordinate2D regionCenter;
- regionCenter.latitude = (min.latitude + max.latitude) / 2.0;
- regionCenter.longitude = (min.longitude + max.longitude) / 2.0;
- CLLocationDistance latMeters = [locSouthEast getDistanceFrom:locNorthEast];
- CLLocationDistance lonMeters = [locSouthEast getDistanceFrom:locSouthWest];
- MKCoordinateRegion region;
- region = MKCoordinateRegionMakeWithDistance(regionCenter, latMeters, lonMeters);
- MKCoordinateRegion fitRegion = [mapView regionThatFits:region];
- [mapView setRegion:fitRegion animated:YES];
- [locSouthWest release];
- [locSouthEast release];
- [locNorthEast release];
- }
Add Comment
Please, Sign In to add comment