在MapKit中缩小和拟合所有注释的最佳方法是什么 [英] What's the best way to zoom out and fit all annotations in MapKit

查看:116
本文介绍了在MapKit中缩小和拟合所有注释的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,如果注释是西北的用户,它缩小了。但是如果注释是用户的东南方,它只会缩小到足以让它们被切断,你必须手动缩小一点。这是我正在使用的代码,我在SO上发现的其他一些代码的变体。

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.longitudeDelta = 7.0;

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

        [locSouthWest release];
        [locNorthEast release];

MapKit中是否有更好的方法可以缩小以便两个注释都有,让我们说一半它们在外框架之间有一英寸?我真的不在乎用户是否需要放大,我只是想让它正确缩小。

Is there a better way built into MapKit to just zoom out so that both annotations have, lets say half an inch between them at the outer frame? I don't really care if the user has to zoom in after, I just want it to zoom out properly.

推荐答案

-(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/

(一直使用它。)

这篇关于在MapKit中缩小和拟合所有注释的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆