ios – 在MapKit中放大并适合所有注释的最佳方式是什么?

前端之家收集整理的这篇文章主要介绍了ios – 在MapKit中放大并适合所有注释的最佳方式是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
快速背景我所做的事情. UIMapView加载并显示单个注释(从不多于一个).在菜单栏上有一个“定位我”按钮,点击“用户位置”即可找到并显示为第二个注释.然后,我将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];

有没有更好的方法内置MapKit只是缩小,以便两个注释,让他们说在他们之间的半帧在外框?我真的不在乎用户是否必须放大,我只是想要正确缩小.

解决方法

-(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];
}

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

(一直使用它.)

原文链接:https://www.f2er.com/iOS/337515.html

猜你在找的iOS相关文章