2010-10-27 62 views
1

我已經制定了下面的代碼:Mapkit - 縮放級別不斷復位

-(void)viewDidLoad 
{ 
    //Set Zoom level using Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = 0.05; 
    span.longitudeDelta = 0.05; 
    region.span = span; 
} 


-(void)locationChange:(CLLocation *)newLocation: (CLLocation *)oldLocation 
{ 
    // This zooms in on the users current loation. 
    curlocation = newLocation.coordinate; 
    region.center = curlocation; 

    [_mapView setRegion:region animated:TRUE]; 
} 

最初的縮放級別設置爲每個在viewDidLoad中的代碼。如何存儲用戶放大或縮小的縮放級別ID,因爲每次接收到新的位置更新時縮放級別都將被重置。

是否有檢測用戶放大或縮小的方法?

UPDATE

我已經添加regionDidChangeAnimated方法如下:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    NSLog(@"Region DID change. Center is now %f,%f, Deltas=%f,%f", 
      region.center.latitude, region.center.longitude, 
      region.span.latitudeDelta, region.span.longitudeDelta); 
} 

日誌中輸出的樣子:

2010-11-01 15:17:29.317 Legginit [2948:307]地區DID更改。
Center現在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:30.553 Legginit [2948:307] Region DID change。
Center現在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:31.063 Legginit [2948:307] Region DID change。
中心現已54.181150,-8.483177,德爾塔= 0.050000,0.050000

2010-11-01 15:17:31.653 Legginit [2948:307]的整數區域DID變化。
Center現在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:32.582 Legginit [2948:307] Region DID change。
Center現在是54.181150,-8.483177,Deltas = 0.050000,0.050000

2010-11-01 15:17:33.608 Legginit [2948:307] Region DID change。
Centre現在是54.181150,-8.483177,Deltas = 0.050000,0.050000

當我放大電話時,我期待Delta值發生變化,但仍保持在0.05。我誤解了這是如何工作的。我認爲我可以捕獲Delta值並存儲它們,這樣如果用戶退出並重新輸入地圖,我可以重置縮放級別。

問候, 斯蒂芬

回答

2

在locationChange,嘗試調用setRegion之前更新區域的跨度爲_mapView的電流量程:

region.span = _mapView.region.span; 

如果你真的需要檢測變焦變化,因爲它們發生,實現MKMapViewDelegate方法regionWillChangeAnimated或regionDidChangeAnimated。

編輯:
您需要從的MapView獲得新的地區和您的區域實例變量設置爲它(地圖視圖不會自動設置您的實例變量):

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    region = mapView.region; //<-- add this 

    NSLog(@"Region DID change. Center is now %f,%f, Deltas=%f,%f", 
      region.center.latitude, region.center.longitude, 
      region.span.latitudeDelta, region.span.longitudeDelta); 
} 
+0

aBit顯然,我走下了使用regionWillChangeAnimated的道路。我上面更新了我的問題,如果你有一分鐘​​,你能否快速查看。我不認爲我完全理解它是如何工作的。 – Stephen 2010-11-01 15:41:07

+0

我已經更新了答案。 – Anna 2010-11-01 16:15:31

0

相反使用該區域,只需更改中心座標。我發現設置區域本身可以稍微改變縮放級別,即使告訴mapView使用與當前mapView相同的區域。

[_mapView setCenterCoordinate:newLocation.coordinate animated:YES];