iOS7 で MKMapView のリージョンに問題が発生しました。
アプリが起動し、デバイスが UI の向きを数回変更した後、map.region によって返される値が非常に奇妙になります。それらは、適切な経度スパンを持ち、緯度スパンが小さい、またはその逆である傾向があり、まるでマップの境界が画面の 1 つの端に沿って狭い長方形に切り詰められた印象を受けているかのようです。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;
}