Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS7中更改方向后,MKMapView区域将断开_Ios_Mapkit_Ios7_Region - Fatal编程技术网

在iOS7中更改方向后,MKMapView区域将断开

在iOS7中更改方向后,MKMapView区域将断开,ios,mapkit,ios7,region,Ios,Mapkit,Ios7,Region,我们在iOS7中的MKMapView区域遇到了问题 在我们的应用程序启动并且设备多次更改UI方向后,map.region返回的值变得非常奇怪。它们往往有一个合理的经度跨度,但有一个小的纬度跨度,反之亦然,就好像地图的边界被沿着屏幕的一个边缘切割成一个狭窄的矩形。发生这种情况后,MKMapView的实际边界和帧仍然是合理的 通过从地图的实际边界计算出我们自己的一个区域,可以解决由此引起的一些问题,但我们仍然有一些问题无法解决。例如,当点击注释以显示其标注时,地图有时会平移以将标注移动到其认为占据的

我们在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;

}
+(void)setMap:(MKMapView*)映射区域:(MKCoordinateRegion)区域
{
CGRect realBounds=map.bounds;
MKCoordinateRegion claimdregion=map.region;//贴图的声明区域,在ios7中旋转后会有惊人的不同
CGRect claimedBounds=[map convertRegion:claimedRegion toRectToView:map];//映射认为其区域占据的边界
//如果我们想把这个区域映射到realBounds,但映射认为它只是claimedBounds大,那又怎么样
//缩小区域将映射到claimedBounds?
MKCoordinateRegion reducedRegion=[Utilities sliceRegion:region inBounds:realBounds to ReducedBunds:claimedBounds];
[地图设置区域:简化区域动画:是];
}
+(MKCoordinateRegion)切片区域:(MKCoordinateRegion)边界内的大区域:(CGRect)整个边界还原边界:(CGRect)还原边界
{
Mk坐标区域还原区域;
//我们地区的角坐标(长/长)
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.lation-bigRegion.span.latitudeDelta/2.0;
//以像素为单位的边界坐标
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;
//以像素为单位的较小边界坐标
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;
//现在计算出缩减边界角的lat和long值是多少
CLLocationDegrees newLeft=左+(右-左)*(简化左-全左)/(全右-全左);
CLLocationDegrees newRight=左+(右-左)*(简化右-全左)/(全右-全左);
CLLocationDegrees newTop=顶部+(底部-顶部)*(简化顶部-整体顶部)/(整体底部-整体顶部);
CLLocationDegrees newBottom=顶部+(底部-顶部)*(简化底部-wholeTop)/(wholeBottom-wholeTop);
reducedRegion.center.longitude=(newRight+newLeft)/2.0;
简化的region.center.latitude=(newBottom+newTop)/2.0;
reducedRegion.span.longitudeDelta=newRight-newLeft;
reducedRegion.span.latitudeDelta=newTop-newBottom;
返回还原区;
}
+(MKCoordinateRegion)getMapRegion:(MKMapView*)地图
{
CGRect边界=map.bounds;
MKCoordinateRegion region=[map convertRect:bounds to region FromView:map];//我们可以在屏幕上看到的区域,而不是地图的不稳定区域!
如果((region.span.latitudelta<0.0)| |(region.span.longitudeDelta<0.0)| region.span.longitudeDelta/region.span.latitudelta>5.0 | region.span.latitudelta/region.span.longitudeDelta>5.0)
{
LogD(@“getMap:region:bad span-lat:%f,long:%f”,region.span.latitudelta,region.span.longitudeDelta);
}
返回区;
}
+(void)setMap:(MKMapView*)地图中心:(CLLocationCoordinate2D)中心
{
CGRect边界=map.bounds;
MKCoordinateRegion boundsRegion=[map convertRect:bounds to region FromView:map];//我们可以在屏幕上看到的区域
MKCoordinateRegion claimdregion=map.region;//贴图的声明区域,在ios7中旋转后会有惊人的不同
CLLocationCoordinate2D offsetCenter;//创建一个值,告诉地图到中心,使其真正居中
offsetCenter.latitude=center.latitude-(boundsRegion.center.latitude-claimedRegion.center.latitude);
offsetCenter.longitude=center.longitude-(boundsRegion.center.longitude-claimdregion.center.longitude);
[地图设置中心坐标:偏移中心动画:是];
}
+(CLLocationCoordinate2D)getMapCenter:(MKMapView*)地图
{
CGRect边界=map.bounds;
MKCoordinateRegion boundsRegion=[map convertRect:bounds to region FromView:map];//我们可以在屏幕上看到的区域
返回boundsRegion.center;
}

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

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

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


您是否尝试过全屏运行地图视图?这意味着没有状态栏,没有导航和工具栏?对于iOS 7中的半透明条,MKMapView会对布局指南进行一些神奇的计算。请看我对这个问题的回答:谢谢你。我不知道
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];
    }