iOS 6 MkMapView在更改区域时保留旋转



在iOS 6中,我试图实现在不更改旋转的情况下更改MkMapView区域的功能。

基本上,我需要能够移动地图来显示区域(并因此设置缩放),但当我调用[mapView setRegion:]时,我也不想旋转地图。

[mapView setCenterCoordinate:]运行良好,但不允许我更改缩放级别。

在iOS 7中,我使用[mapView setCamera:],其中我有一个指定了中心坐标和缩放级别的相机。。。我基本上需要iOS 6中的这个功能。

有什么想法吗?谢谢

我也遇到了同样的问题,最终完全放弃了[mapView setRegion:]方法,转而使用[mapView setCamera:],将原始区域和航向作为相机定向的基础。

MKCoordinateRegion currentRegion = MKCoordinateRegionMake(center, span);
double altitude = [self determineAltitudeForMapRect:MKMapRectForCoordinateRegion(currentRegion) withHeading:_heading andWithViewport:[[UIScreen mainScreen] bounds].size];
MKMapCamera *currentCamera = [MKMapCamera new];
[currentCamera setHeading:_heading];
[currentCamera setCenterCoordinate:center];
[currentCamera setAltitude:altitude];
[_mapView setCamera:currentCamera];

这个选项的诀窍是如何确定[currentCamera setAltitude:]值,该值通常会用[mapView setRegion:]自动设置

我的解决方案是对这个答案进行调整https://stackoverflow.com/a/21034410/1130983它使用一些简单的三角来确定海拔高度,假设卡马拉地图的视角约为30度。然而,我不是在多边形中传递,而是直接在MKMapRect中传递:

- (double)determineAltitudeForMapRect:(MKMapRect)boundingRect withHeading:(double)heading andWithViewport:(CGSize)viewport
{
// Get a bounding rectangle that encompasses the polygon and represents its
// true aspect ratio based on the understanding of its heading.
MKCoordinateRegion boundingRectRegion = MKCoordinateRegionForMapRect(boundingRect);
// Calculate a new bounding rectangle that is corrected for the aspect ratio
// of the viewport/camera -- this will be needed to ensure the resulting
// altitude actually fits the polygon in view for the observer.
CLLocationCoordinate2D upperLeftCoord = CLLocationCoordinate2DMake(boundingRectRegion.center.latitude + boundingRectRegion.span.latitudeDelta / 2, boundingRectRegion.center.longitude - boundingRectRegion.span.longitudeDelta / 2);
CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake(boundingRectRegion.center.latitude + boundingRectRegion.span.latitudeDelta / 2, boundingRectRegion.center.longitude + boundingRectRegion.span.longitudeDelta / 2);
CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake(boundingRectRegion.center.latitude - boundingRectRegion.span.latitudeDelta / 2, boundingRectRegion.center.longitude - boundingRectRegion.span.longitudeDelta / 2);
CLLocationDistance hDist = MKMetersBetweenMapPoints(MKMapPointForCoordinate(upperLeftCoord), MKMapPointForCoordinate(upperRightCoord));
CLLocationDistance vDist = MKMetersBetweenMapPoints(MKMapPointForCoordinate(upperLeftCoord), MKMapPointForCoordinate(lowerLeftCoord));
double adjacent;
if (boundingRect.size.height > boundingRect.size.width)
{
adjacent = vDist / 2;
}
else
{
adjacent = hDist / 2;
}
double result = adjacent / tan(DEGREES_TO_RADIANS(15));
return result;
}

最新更新