MKMapView - 缩放以适合用户位置周围的最近注释

时间:2014-02-06 15:19:45

标签: ios iphone mkmapview

我想缩放我的mapview以显示最近注释的至少一个注释,具有最高可能的缩放和用户位置。我尝试过以下方法:

-(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个最近的距离并根据该信息创建一个区域?

2 个答案:

答案 0 :(得分:3)

如果您正在为iOS 7及更高版本开发,您可以保存按用户位置和注释之间的距离排序的注释数组,然后获取前3个注释。完成后,您可以使用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);

当然,您必须更新第二个解决方案才能使用最近的注释点,但您已经知道如何找到这些注释点,这样就不会出现问题。

答案 1 :(得分:2)

将注释存储在数组中

添加此功能并根据需要更改距离

- (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations
{
MKCoordinateRegion region;

if ([annotations count] == 0)
{
    region = MKCoordinateRegionMakeWithDistance(_mapView.userLocation.coordinate, 1000, 1000);
}

else if ([annotations count] == 1)
{
    id <MKAnnotation> annotation = [annotations lastObject];
    region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
} else {
    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for (id <MKAnnotation> annotation in annotations)
    {
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
    }

    const double extraSpace = 1.12;
    region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) / 2.0;
    region.center.longitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude) / 2.0;
    region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace;
    region.span.longitudeDelta = fabs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace;
}

return [self.mapView regionThatFits:region];
}

并像这样称呼它

 MKCoordinateRegion region = [self regionForAnnotations:_locations];
[self.mapView setRegion:region animated:YES];

现在缩放应该适合数组中的所有注释

祝你好运!!