MKMapView 区域在 iOS7 中方向更改后损坏

Posted

技术标签:

【中文标题】MKMapView 区域在 iOS7 中方向更改后损坏【英文标题】:MKMapView region becomes broken after orientation change in iOS7 【发布时间】:2013-09-25 15:40:35 【问题描述】:

我们在 ios7 中遇到了 MKMapView 的区域问题。

在我们的应用程序启动并且设备改变了几次 UI 方向之后,map.region 返回的值变得非常奇怪。它们往往具有合理的经度跨度,但纬度跨度较小,反之亦然,就好像地图的印象是其边界已沿屏幕的一个边缘缩小为一个狭窄的矩形。发生这种情况后,MKMapView 的实际边界和框架仍然是合理的。

可以通过根据地图的实际边界计算我们自己的区域来解决由此引起的一些问题,但我们仍有一些问题无法解决。例如,当点按注释以调出其标注时,地图有时会平移以将标注移动到它认为占据的屏幕的一小部分。

有其他人遇到过这个问题吗?

如果它们有用的话,我们实现该变通方法的垫片如下:

+(void)setMap: (MKMapView*) map region:(MKCoordinateRegion) region



CGRect realBounds = map.bounds;

MKCoordinateRegion claimedRegion = map.region; // the map's claimed region, which is wonkily different after a rotate in ios7

CGRect claimedBounds = [map convertRegion:claimedRegion toRectToView:map]; // the bounds which the map thinks its region occupies


// if we want region to map to realBounds, but the map thinks it is only claimedBounds big, what
// reduced region will map to claimedBounds ?

MKCoordinateRegion reducedRegion = [Utilities sliceRegion: region inBounds: realBounds toReducedBounds: claimedBounds];

[map setRegion:reducedRegion animated:YES];



+ (MKCoordinateRegion) sliceRegion: (MKCoordinateRegion) bigRegion inBounds: (CGRect) wholeBounds toReducedBounds: (CGRect) reducedBounds

MKCoordinateRegion reducedRegion;


 // Coords of our region's corners in lat/long
CLLocationDegrees left = bigRegion.center.longitude - bigRegion.span.longitudeDelta/2.0;
CLLocationDegrees right = bigRegion.center.longitude + bigRegion.span.longitudeDelta/2.0;
CLLocationDegrees top = bigRegion.center.latitude + bigRegion.span.latitudeDelta/2.0;
CLLocationDegrees bottom = bigRegion.center.latitude - bigRegion.span.latitudeDelta/2.0;


// Coords of our bounds in pixels
CGFloat wholeLeft = wholeBounds.origin.x;
CGFloat wholeRight = wholeBounds.origin.x + wholeBounds.size.width;
CGFloat wholeTop = wholeBounds.origin.y;
CGFloat wholeBottom = wholeBounds.origin.y + wholeBounds.size.height;

// Coords of the smaller bounds in pixels
CGFloat reducedLeft = reducedBounds.origin.x;
CGFloat reducedRight = reducedBounds.origin.x + reducedBounds.size.width;
CGFloat reducedTop = reducedBounds.origin.y;
CGFloat reducedBottom = reducedBounds.origin.y + reducedBounds.size.height;

// Now work out what the lat & long values for the corners of the reduced bounds are
CLLocationDegrees newLeft = left + (right-left) * (reducedLeft - wholeLeft) / (wholeRight - wholeLeft);
CLLocationDegrees newRight = left + (right-left) * (reducedRight - wholeLeft) / (wholeRight - wholeLeft);
CLLocationDegrees newTop = top + (bottom - top) * (reducedTop - wholeTop) / (wholeBottom - wholeTop);
CLLocationDegrees newBottom = top + (bottom - top) * (reducedBottom - wholeTop) / (wholeBottom - wholeTop);

reducedRegion.center.longitude = (newRight + newLeft) / 2.0;
reducedRegion.center.latitude = (newBottom + newTop) / 2.0;

reducedRegion.span.longitudeDelta = newRight - newLeft;
reducedRegion.span.latitudeDelta = newTop - newBottom;


return reducedRegion;



+(MKCoordinateRegion)getMapRegion: (MKMapView*) map



CGRect bounds = map.bounds;

MKCoordinateRegion region = [map convertRect:bounds toRegionFromView:map]; // the region we can see on the screen, not the map's wonky region!

if ((region.span.latitudeDelta < 0.0) || (region.span.longitudeDelta < 0.0) ||  region.span.longitudeDelta / region.span.latitudeDelta > 5.0 || region.span.latitudeDelta / region.span.longitudeDelta > 5.0 )

    LogD(@"getMap: region: bad span -  lat: %f, long: %f", region.span.latitudeDelta, region.span.longitudeDelta);


return region;


+(void)setMap: (MKMapView*) map center: (CLLocationCoordinate2D) center



CGRect bounds = map.bounds;

MKCoordinateRegion boundsRegion = [map convertRect:bounds toRegionFromView:map]; // the region we can see on the screen

MKCoordinateRegion claimedRegion = map.region; // the map's claimed region, which is wonkily different after a rotate in ios7

CLLocationCoordinate2D offsetCenter; // make up a value to tell the map to center on which will make it really center

offsetCenter.latitude = center.latitude - ( boundsRegion.center.latitude - claimedRegion.center.latitude );
offsetCenter.longitude = center.longitude - ( boundsRegion.center.longitude - claimedRegion.center.longitude );

[map setCenterCoordinate:offsetCenter animated:YES];





+(CLLocationCoordinate2D)getMapCenter: (MKMapView*) map


CGRect bounds = map.bounds;

MKCoordinateRegion boundsRegion = [map convertRect:bounds toRegionFromView:map]; // the region we can see on the screen
return boundsRegion.center;


【问题讨论】:

您是否尝试过全屏运行地图视图?这意味着没有状态栏,也没有导航和工具栏?对于 iOS 7 中的半透明条,MKMapView 对布局指南进行了一些神奇的计算。请参阅我对这个问题的回答:***.com/questions/18903808/… 谢谢。我不认为这是我们的问题,因为直到纵向-横向转换发生了几次之后,我们才发现我们的问题。 请记住,topLayoutGuide 在旋转时会发生变化,因为导航栏会改变其高度。我发现即使您更改状态栏的可见性,地图的坐标中心也会移动。 谢谢 - 这帮助我们解决了中心问题。但它与我们一开始的更一般的地图区域错误非常不同,后者更难以预测并导致更大的居中错误。 【参考方案1】:

当我在 iOS 7 上的 LandscapeRight 中设置地图区域时,一切正常。

当我将设备旋转到 LandscapeLeft 并加载相同的区域时,地图会大幅移动并放大到远处。缩放级别需要乘以 100 才能解决问题,即。 50000 变为 5000000 并且我需要从 lat 减去 23 并将 3 添加到 lon 即。 (41.0, 29.0) 变为 (18.0, 32.0)。

经过一些测试,我能够解决 iOS 7 和 iOS 6 的问题(请原谅 iOS 版本检查它的快速和肮脏)

if([[[UIDevice currentDevice] systemVersion] rangeOfString:@"7."].location != NSNotFound)
        if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(41.0, 29.0);
            [_mapView setRegion:MKCoordinateRegionMakeWithDistance(startCoord, 5000000.0, 5000000.0) animated:NO];
        else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(18.0, 32.0);
            [_mapView setRegion:MKCoordinateRegionMakeWithDistance(startCoord, 50000.0, 50000.0) animated:NO];
        

    else
        MKCoordinateRegion region;
        region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.3;
        region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.7;
        region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * span; // Add a little extra space on the sides
        region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * span; // Add a little extra space on the sides

        region = [_mapView regionThatFits:region];
        [_mapView setRegion:region animated:NO];
    

【讨论】:

以上是关于MKMapView 区域在 iOS7 中方向更改后损坏的主要内容,如果未能解决你的问题,请参考以下文章

当mapView为3D时,iOS MapKit获取MKMapView的实际可见区域

如何更改 iOS7 中的未选定的 tabbaritem 颜色

GoogleMap(Android)投影可见区域在方向更改后未更新?

MKMapView - 未调用 rendererForOverlay

如何更改 iOS7 中的未选定的 tabbaritem 颜色

在 iOS 7 中添加 MKMapView 会导致状态栏问题