【发布时间】:2015-01-21 16:59:40
【问题描述】:
我有一个全屏地图,我想缩放到所有注释都在地图上可见的级别,但仅在应用程序的内容区域中可见,如下图所示:
如您所见,地图填满了整个屏幕,但是,顶部和底部覆盖了覆盖地图的矩形。内容区域是亮绿色的部分。给定一组注释,我希望能够缩放地图,以便所有内容在绿色内容区域内可见,而不是在地图的实际框架内。
我在这里使用这个(类别)方法,它执行缩放以适应地图框架的默认任务。但是我在如何修改它以仅考虑内容区域时陷入困境:
- (void)zoomToShowAnnotations:(NSArray *)annotations extraPaddingMultiplier:(CGFloat)multiplier
{
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * (multiplier != 0 ? multiplier : 1); // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * (multiplier != 0 ? multiplier : 1); // Add a little extra space on the sides
[self setRegion:region animated:YES];
}
【问题讨论】:
-
setVisibleMapRect:edgePadding:animated 方法应该使这更容易。