2012-01-17 142 views
0

我問我的MapView顯示用戶的當前位置,我從CLLocationManager使用startUpdatingLocation的委託獲取它的用戶的當前位置,我也向MapView添加了一些註釋,但問題是滾動地圖時顯示一些註釋地圖返回到當前用戶MapView返回到當前位置註解?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Get the location if the user 
    _locationManager = [[CLLocationManager alloc] init]; 

    if ([CLLocationManager locationServicesEnabled]) 
    { 
     _locationManager.delegate = self; 
     [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; 
     [_locationManager startUpdatingLocation]; 
    } 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
     // set the map view to the current location 
     MKCoordinateSpan span; 
     span.latitudeDelta = 0.01; 
     span.longitudeDelta = 0.05; 

     MKCoordinateRegion region; 
     region.span = span; 
     region.center = _locationManager.location.coordinate; 

     [self.mapView setRegion:region animated:YES]; 
} 

回答

1

如果獲得更準確的或最近的位置或很明顯,如果該裝置被移動的didUpdateToLocation委託方法可以被調用多次的註釋。

由於您無條件地在該委託方法中設置地圖的區域,所以即使在用戶或應用程序平移或將地圖放大到其他地方後,地圖也會始終回到用戶位置。

在該委託方法中,您可以通過添加一個布爾ivar來縮放到用戶位置一次,以告知您是否已經縮放。

另一種選擇是檢查location的各種屬性,如timestamphorizontalAccuracy,並且只有在這些值處於特定閾值時才返回到用戶位置。

0

你必須調用下面的方法[_locationManager stopUpdatingLocation];類似以下內容: -

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
     [_locationManager stopUpdatingLocation]; 

     // set the map view to the current location 
     MKCoordinateSpan span; 
     span.latitudeDelta = 0.01; 
     span.longitudeDelta = 0.05; 

     MKCoordinateRegion region; 
     region.span = span; 
     region.center = _locationManager.location.coordinate; 

     [self.mapView setRegion:region animated:YES]; 
} 

因爲一旦你開始更新位置...它調用此函數後調用上面的函數每time..so你需要停下來調用這個通過調用[_locationManager stopUpdatingLocation];方法來實現。

相關問題