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

空扰寡人 提交于 2019-11-28 16:07:33
-(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(MapAnnotation* annotation in mapView.annotations)
    {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);

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

Taken from: http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

(Use it all the time.)

In iOS7 there's a method that does just that. Just call:

[yourMapView showAnnotations:yourAnnotationsArray animated:YES];
Mahesh

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;

}
Azhar

You can use this code to show all annotations

MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations)
{
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
    zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[mapView setVisibleMapRect:zoomRect animated:YES];

if you want to include user location just replace two lines below with the first line of above code

MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!