在MapKit中缩小和拟合所有注释的最佳方法是什么

Mik*_*iel 14 iphone cocoa-touch objective-c mapkit ios

快速了解我的情况.UIMapView加载并显示单个注释(从不多于一个).在菜单栏上,有一个Locate Me按钮,点击查找userLocation并显示为第二个注释.然后我将MapView缩小以适应范围内的那些注释,但我无法找到合适的方法.根据第一个注释相对于用户位置的位置,有时它不会缩小.

例如,如果注释位于用户的西北方向,则会缩小范围.但是如果注释是用户的东南方,它只会缩小到足以让它们被切断,你必须手动缩小一点.这是我正在使用的代码,我在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.longitudeDelta = 7.0;

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

        [locSouthWest release];
        [locNorthEast release];
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法内置到MapKit只是缩小,以便两个注释都有,让他们说在外框架之间半英寸?如果用户必须放大后我真的不在乎,我只是想让它正确缩小.

Eva*_*ski 32

-(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];
}
Run Code Online (Sandbox Code Playgroud)

摘自:http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/

(一直使用它.)


kan*_*tar 9

在iOS7中,有一种方法可以做到这一点.只需致电:

[yourMapView showAnnotations:yourAnnotationsArray animated:YES];
Run Code Online (Sandbox Code Playgroud)