2014-01-30 34 views
4

在iOS 6中,我試圖實現在不更改旋轉的情況下更改MkMapView區域的功能。iOS 6 MkMapView在更改區域時保持旋轉

基本上,我需要能夠移動地圖來顯示地區(因此設置縮放),但我也不想旋轉地圖時,我打電話[mapView setRegion:]

[mapView setCenterCoordinate:]效果不錯,但不允許我改變縮放級別。

在iOS系統7,我用[mapView setCamera:],在那裏我有中心的攝像機座標和指定的縮放級別...基本上,我需要在IOS 6

任何想法此功能?謝謝!

回答

1

我有這個完全相同的問題,並最終完全放棄了[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; 
}