2012-01-17 2 views
0

MapView에 startUpdatingLocation의 대리자를 사용하여 CLLocationManager에서 가져온 사용자의 현재 위치를 표시하도록 요청하고 MapView에 일부 주석을 추가하지만 문제는지도에서 일부 주석을 표시하기 위해 스크롤 할 때입니다. 보다 정확한이나 최근의 위치가를 얻거나 분명 장치가 이동되는 경우 경우 didUpdateToLocation 대리자 메서드를 여러 번 호출 할 수 있습니다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

현재 사용자의 주석에지도 리턴.

위임 메서드에서 무조건지도 영역을 설정하므로 사용자 또는 앱이지도를 다른 곳으로 이동하거나 확대 한 후에도지도가 항상 업데이트를 가져올 때 다시 사용자 위치로 이동합니다.

위임 메서드에서 이미 확대/축소되어 있는지 여부를 알려주는 bool ivar를 추가하여 사용자 위치를 한 번만 확대 할 수있었습니다.

다른 옵션은 timestamp 또는 horizontalAccuracy과 같이 location의 다양한 속성을 확인하고 해당 값이 특정 임계 값에있는 경우에만 사용자 위치로 돌아갑니다.

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]; 메서드 호출하여.