我有這個完全相同的問題,並最終完全放棄了[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它使用了一些簡單的三角函數來決定高度,假設地圖camara具有大約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;
}