Zoom to fit region for all annotations - ending up zooming in between annotations

Posted by Krismutt on Stack Overflow See other posts from Stack Overflow or by Krismutt
Published on 2010-12-24T01:02:59Z Indexed on 2010/12/24 11:54 UTC
Read the original article Hit count: 625

Hey everybody!!

I have a problem with fitting all my annotations to the screen... sometimes it shows all annotations, but some other times the app is zooming in between the two annotations so that none of them are visible... I want the app to always fit the region to the annotations and not to zoom in between them... what do I do wrong?

if ([mapView.annotations count] == 2) { 
 CLLocationCoordinate2D SouthWest = location;
 CLLocationCoordinate2D NorthEast = savedPosition;

 NorthEast.latitude = MAX(NorthEast.latitude, savedPosition.latitude);
 NorthEast.longitude = MAX(NorthEast.longitude, savedPosition.longitude); 

 SouthWest.latitude = MIN(SouthWest.latitude, location.latitude);
 SouthWest.longitude = MIN(SouthWest.longitude, location.longitude);

 CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:SouthWest.latitude longitude:SouthWest.longitude];
 CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:NorthEast.latitude longitude:NorthEast.longitude];

 CLLocationDistance meter = [locSouthWest distanceFromLocation:locNorthEast];

 MKCoordinateRegion region;
 region.span.latitudeDelta = meter / 111319.5;
 region.span.longitudeDelta = 0.0;
 region.center.latitude = (SouthWest.latitude + NorthEast.latitude) / 2.0;
 region.center.longitude = (SouthWest.longitude + NorthEast.longitude) / 2.0;

 region = [mapView regionThatFits:region];
 [mapView setRegion:region animated:YES];

 [locSouthWest release];
 [locNorthEast release]; 
}

Any ideas?

NEW CODE (by Satya)

-(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(FSMapAnnotation* annotation in mapView.annotations)
        {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, location.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, location.latitude);

            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, savedPosition.longitude);
            bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, savedPosition.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];
    }

Can't get it to work... FSMapAnnoation is undeclared... how do I fix this?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about mapkit