2012-07-05 38 views
0

我有一個關於地圖和委託的問題。我做一本書一個鍛鍊和它說,我使用的方法MKMapKit iOS 5.我可以在需要時設置所需的委託嗎?

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    //CLLocationCoordinate2D loc = [userLocation coordinate]; 
    self.coord2D = [userLocation coordinate]; 

    //MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord2D, 250, 250); 

    [worldView setRegion:region animated:YES]; 
} 

這意味着每次我提出我的電話從我的位置時,它會顯示自己在屏幕上。我還創建了一個調用此方法的按鈕。因此,如果我滾動我的地圖,我可以看到我在哪裏。

問題是:如果我在一輛汽車上並想滾動我的地圖,這將變成一項艱鉅的任務,因爲此方法將在我改變我的位置時隨時致電。

有沒有其他的方法,或者如果我有一個選項來激活和停用委託?

在此先感謝。

回答

1

據我所知,這很簡單。只需要一個按鈕來激活和關閉它。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    //CLLocationCoordinate2D loc = [userLocation coordinate]; 
    self.coord2D = [userLocation coordinate]; 

    if (self.trackUserLocation) { 
     //MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250); 
     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord2D, 250, 250); 

     [worldView setRegion:region animated:YES]; 
    } 
} 

屬性:

@property (nonatomic, assign) BOOL trackUserLocation; 

中的按鈕操作:

self.trackUserLocation = !self.trackUserLocation; 

你不必使它比任何困難。

+0

什麼是簡單的解決方案。非常感謝 – Camus

相關問題