缩放MKMapView以适合注释图钉?

我正在使用MKMapView,并在大约5-10公里的区域中向地图添加了许多注释图钉。当我运行该应用程序时,我的地图开始缩小以显示整个世界,什么是最好的缩放地图方式以使图钉适合视图?


编辑: 我最初的想法是使用MKCoordinateRegionMake并从我的注释中计算坐标中心,longitudeDelta和latitudeDelta。我很确定这是可行的,但是我只是想检查一下我是否没有遗漏任何明显的东西。


添加的代码,顺便说一句:FGLocation是符合的类MKAnnotation,locationFake是NSMutableArray这些对象之一。总是欢迎评论....


- (MKCoordinateRegion)regionFromLocations {

    CLLocationCoordinate2D upper = [[locationFake objectAtIndex:0] coordinate];

    CLLocationCoordinate2D lower = [[locationFake objectAtIndex:0] coordinate];


    // FIND LIMITS

    for(FGLocation *eachLocation in locationFake) {

        if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude;

        if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude;

        if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude;

        if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude;

    }


    // FIND REGION

    MKCoordinateSpan locationSpan;

    locationSpan.latitudeDelta = upper.latitude - lower.latitude;

    locationSpan.longitudeDelta = upper.longitude - lower.longitude;

    CLLocationCoordinate2D locationCenter;

    locationCenter.latitude = (upper.latitude + lower.latitude) / 2;

    locationCenter.longitude = (upper.longitude + lower.longitude) / 2;


    MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);

    return region;

}


慕妹3146593
浏览 610回答 3
3回答

萧十郎

你说对了。找到最大和最小纬度和经度,应用一些简单的算法,然后使用MKCoordinateRegionMake。对于iOS 7及更高版本,请使用showAnnotations:animated:,来自MKMapView.h:// Position the map such that the provided array of annotations are all visible to the fullest extent possible.&nbsp;- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0);

偶然的你

这是我在这里为我工作的那个:(编辑:我已经使用@Micah的建议更新了解决方案,以将pointRect增加0.1以确保rect最终不会变得非常小!)MKMapRect zoomRect = MKMapRectNull;for (id <MKAnnotation> annotation in mapView.annotations){&nbsp; &nbsp; MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);&nbsp; &nbsp; MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);&nbsp; &nbsp; zoomRect = MKMapRectUnion(zoomRect, pointRect);}[mapView setVisibleMapRect:zoomRect animated:YES];&nbsp;您还可以通过将第一行替换为以下内容来更新它以包括userLocation引脚:MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);

阿晨1998

苹果为IOS 7添加了新方法,以简化生活。[mapView showAnnotations:yourAnnotationArray animated:YES];您可以轻松地从存储在地图视图中的数组中提取:yourAnnotationArray = mapView.annotations;并快速调整相机!mapView.camera.altitude *= 1.4;除非用户安装了iOS 7+或OS X 10.9+,否则此功能将无效。在此处查看自定义动画
打开App,查看更多内容
随时随地看视频慕课网APP