Alex Rivera | Logout

What's the best way to zoom out and fit all annotations in MapKit

Asked 2010-11-12T22:01:19.693
15

Quick background of what I have going on. UIMapView loads and shows a single annotation (Never more than one). On the menu bar, there is a Locate Me button, on tap the userLocation is found and displayed as a second annotation. I then I have MapView zoom out to fit both those annotations in range but I am unable to find a suitable way of doing so. Depending on where the first annotation is placed in relation to the user location, sometimes it doesn't zoom out enough.

For example, if the annotation is North West of the User, it zooms out fine. But if the annotation is South East of the User, it only zooms out enough that they are just getting cut off and you have to manually zoom out a bit more. Here's the code I am using, variation of some others I have found on SO.

        CLLocation *whereIAm = mapView.userLocation.location;

        float lat = whereIAm.coordinate.latitude;
        float lon = whereIAm.coordinate.longitude;


        CLLocationCoordinate2D southWest = {[currentLatitude floatValue], [currentLongitude floatValue]};
        CLLocationCoordinate2D northEast = southWest;

        southWest.latitude = MIN(southWest.latitude, lat);
        southWest.longitude = MIN(southWest.longitude, lon);

        northEast.latitude = MAX(northEast.latitude, lat);
        northEast.longitude = MAX(northEast.longitude, lon);

        CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
        CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];

        CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];

        MKCoordinateRegion region;
        region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
        region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
        region.span.latitudeDelta = meters / 111319.5
        region.span.longitudeDe
Edit
Report

1 Answer

1

Those who do the monotouch c# coding

BasicMapAnnotation is inherit class from MKAnnotation

private void GetRegion(MKMapView mapView)
{
    var userWasVisible = mapView.ShowsUserLocation;
    mapView.ShowsUserLocation = false; // ignoring the blue blip
    // start with the widest possible viewport
    var tl = new CLLocationCoordinate2D(-90, 180); // top left
    var br = new CLLocationCoordinate2D(90, -180); // bottom right
    foreach (var an in mapView.Annotations)
    {
        // narrow the viewport bit-by-bit
        CLLocationCoordinate2D coordinate = ((BasicMapAnnotation) an).Coordinate;
        tl.Longitude = Math.Min(tl.Longitude, coordinate.Longitude);
        tl.Latitude = Math.Max(tl.Latitude, coordinate.Latitude);
        br.Longitude = Math.Max(br.Longitude, coordinate.Longitude);
        br.Latitude = Math.Min(br.Latitude, coordinate.Latitude);
    }
    var center = new CLLocationCoordinate2D
    {
        // divide the range by two to get the center
        Latitude = tl.Latitude - (tl.Latitude - br.Latitude)*0.5,Longitude = tl.Longitude + (br.Longitude - tl.Longitude)*0.5

    };
    var span = new MKCoordinateSpan
    {
        // calculate the span, with 20% margin so pins aren’t on the edge
        LatitudeDelta = Math.Abs(tl.Latitude - br.Latitude)*1.2,LongitudeDelta = Math.Abs(br.Longitude - tl.Longitude)*1.2

    };
    var region = new MKCoordinateRegion {Center = center, Span = span};
    region = mapView.RegionThatFits(region); // adjusts zoom level too
                mapView.SetRegion(region, true); // animated transition
                mapView.ShowsUserLocation =

    userWasVisible;

}
answered 2013-04-02T01:36:33.000

Your Answer