【发布时间】:2015-07-08 23:16:27
【问题描述】:
我正在尝试缩放以适应地图上的注释,同时锁定中心并提供一些插图。
- (void)fitAnnotations:(NSArray *)annotations edgeInsets:(UIEdgeInsets)insets
{
CLLocationCoordinate2D originalCenter = self.centerCoordinate;
MKMapRect mapRect = MKMapRectNull;
for (id<MKAnnotation> annotation in annotations) {
MKMapPoint p = MKMapPointForCoordinate([annotation coordinate]);
mapRect = MKMapRectUnion(mapRect, MKMapRectMake(p.x, p.y, 0, 0));
}
mapRect = [self mapRectThatFits:mapRect edgePadding:insets];
MKCoordinateRegion mapRegion = MKCoordinateRegionForMapRect(mapRect);
// we now try to go back to the original center, while increasing the span by neccessary amount
MKCoordinateSpan centeringDelta = MKCoordinateSpanMake(fabs(mapRegion.center.latitude - originalCenter.latitude), fabs(mapRegion.center.longitude - originalCenter.longitude));
mapRegion.center = originalCenter;
mapRegion.span.latitudeDelta += centeringDelta.latitudeDelta * 2.0;
mapRegion.span.longitudeDelta += centeringDelta.longitudeDelta * 2.0;
mapRegion = [self regionThatFits:mapRegion];
[self setRegion:mapRegion animated:YES];
}
此处代码的第一部分按预期工作:它在尊重插图的同时缩放以适应。但是,它会移动中心。
之后我尝试重新调整中心,但失败了。我不确定我关于重新居中的数学是否正确。
【问题讨论】:
-
您是否尝试过使用
MKCoordinateRegionMake创建新的地图区域?这将允许您传入centerCoordinate和span -
@rmp 是的,但是当您移动中心时,您确保适合的一些注释现在超出了范围。
-
你应该可以在原来的中心通过而不是移动它
-
如果 (0,0) 上有注释并且我将中心移动 (0, -5),那么顶部注释现在将位于 (0, -5)。这是我要避免的。
-
如果你将新计算的
span与centerCoordinate一起传递,MKCoordinateRegionMake应该处理好这个