gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:54:01 25 4
gpt4 key购买 nike

我想缩放我的 map View 以显示最近注释的至少一个注释,以尽可能高的缩放比例和用户位置。我尝试了以下方法:

-(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 及更高版本进行开发,您可以保存一组按用户位置和注释之间的距离排序的注释,然后获取前 3 个注释。一旦你有了这些,你可以使用showAnnotations:animated:这将定位 map ,以便所有注释都可见。

这是另一种方式(取自 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com