我正在尝试将所有注释放入我的MKMapView
同时将当前用户位置保持在地图中心。
已经有很多参考资料[1][2]介绍如何缩小区域以适合地图上的注释,但它们会调整当前的中心位置,例如如果所有注释都位于我当前用户位置的东部,它将进行调整,以便当前用户位置移动到地图的左侧。
如何缩小地图,使其适合显示的所有注释,但是keep用户当前位置在地图中心?
Refs:
[1] 缩放 MKMapView 以适应注释图钉? https://stackoverflow.com/questions/4680649/zooming-mkmapview-to-fit-annotation-pins
[2] - (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated NS_AVAILABLE(10_9, 7_0);
我发现这个解决方案是最可靠的,@Anna 也建议了它,所以它可能是一个不错的解决方案。
这是我的方法(作为我继承的方法实现MKMapView
- (void)fitAnnotationsKeepingCenter {
CLLocation *centerLocation = [[CLLocation alloc]
initWithLatitude:self.centerCoordinate.latitude
longitude:self.centerCoordinate.longitude];
// starting distance (do not zoom less than this)
CLLocationDistance maxDistance = 350;
for (id <MKAnnotation> vehicleAnnotation in [self annotations]) {
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:vehicleAnnotation.coordinate.latitude longitude:vehicleAnnotation.coordinate.longitude];
maxDistance = MAX(maxDistance, [centerLocation distanceFromLocation:annotationLocation]);
}
MKCoordinateRegion fittedRegion = MKCoordinateRegionMakeWithDistance(centerLocation.coordinate, maxDistance * 2, maxDistance * 2);
fittedRegion = [self regionThatFits:fittedRegion];
fittedRegion.span.latitudeDelta *= 1.2;
fittedRegion.span.longitudeDelta *= 1.2;
[self setRegion:fittedRegion animated:YES];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)