我想缩放地图视图以显示最近注释中的至少一个注释,并尽可能显示最大的缩放比例和用户位置。我尝试了以下方法:

-(void)zoomToFitNearestAnnotationsAroundUserLocation {


    MKMapPoint userLocationPoint = MKMapPointForCoordinate(self.restaurantsMap.userLocation.coordinate);
    MKCoordinateRegion region;
    if ([self.restaurantsMap.annotations count] > 1) {

        for (id<MKAnnotation> annotation in self.restaurantsMap.annotations) {

          MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
          CLLocationDistance distanceBetweenAnnotationsAndUserLocation = MKMetersBetweenMapPoints(annotationPoint, userLocationPoint);


             region =  MKCoordinateRegionMakeWithDistance(self.restaurantsMap.userLocation.coordinate, distanceBetweenAnnotationsAndUserLocation, distanceBetweenAnnotationsAndUserLocation);


        }

        [self.restaurantsMap setRegion:region animated:YES];

    }



}

我如何设法保存2-3个最近的距离并根据该信息创建区域?

最佳答案

如果您正在为iOS 7及更高版本进行开发,则可以保存一个注释数组,按用户位置和注释之间的距离排序,然后获取前三个注释。有了这些标签后,您就可以使用showAnnotations:animated:定位地图了,以便所有注释都可见。

这是另一种方式(取自here):

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

//You could also update this to include the userLocation pin by replacing the first line with
MKMapPoint annotationPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);

当然,您必须更新第二个解决方案以仅使用最接近的注释点,但是您已经知道如何找到这些注释点,因此这不是问题。

关于ios - MKMapView-缩放以适合用户位置附近的最近注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21606828/

10-16 19:01