2013-09-10 3 views
1

Я звоню CLLocationManager, и он также вызывает его метод делегирования. Но моя проблема в том, что не обновляет свое новое местоположение после путешествия в 1 км.CLLocation Manager не обновляет новое местоположение

Вот мой код:

locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLocation1:) userInfo:nil repeats:YES]; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 

// Method did update location - Update location when location change 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    // this method is calling after every 1 sec interval time.. 
} 

-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
{ 
    // this method is not calling once also after travelling around a km ....   
} 

Что я делаю неправильно?

ответ

1

Вы должны позвонить startUpdatingLocation или startMonitoringSignificantLocationChanges в диспетчере местоположений, чтобы он начал проверку на изменение местоположения и вызов методов делегата.

locationManager:didUpdateToLocation:fromLocation: устарел, поэтому вы можете ожидать, что он не всегда будет использоваться.

Если вы вызываете locationManager:didUpdateLocations:, вы получаете обновления местоположения.

0

Ваш первый метод

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    // this method is calling after every 1 sec interval time.. 
} 

используется с прошивкой 6.

Второй является устаревшим с прошивкой 6 и был использован перед прошивкой 6. Вы можете использовать оба метода, в зависимости от системы версии на устройстве, на котором работает ваше приложение, добавив вспомогательный метод.

- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation 
{ 
    [self locationManager:manager helperForLocation:newLocation]; 
} 

- (void) locationManager:(CLLocationManager *) manager didUpdateLocations:(NSArray *) locations 
{ 
    if([locations count] > 0) 
    { 
     [self locationManager:manager helperForLocation:[locations objectAtIndex:[locations count] - 1]]; 
    } 
} 

- (void) locationManager:(CLLocationManager *) manager helperForLocation:(CLLocation *) newLocation 
{ 
     // your code what to do with location goes here 
} 

Место извлекаемого прошивкой 6 заворачивает в списке и может быть больше 1. В моем примере я беру последнюю и положил его в мой помощник.

0

я сделал это в мое приложение с использованием этого

myLocationManager = [[CLLocationManager alloc] init]; 
myLocationManager.delegate = self; 
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;  
[myLocationManager startUpdatingLocation]; 


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] 
          initWithTitle:@"Error" message:@"Failed to Get Your Location, Please turn on GPS and Restart The Application"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
    [errorAlert release]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 

    // Stop Location Manager 
    [myLocationManager stopUpdatingLocation]; 
} 
1
- (void)viewDidLoad 
{ 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLLocationAccuracyKilometer; 
    [locationManager startUpdatingLocation]; 

    CLLocation *location = [locationManager location]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    //[manager stopUpdatingLocation]; 

    CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate]; 

    float latitude_current = newLocation.coordinate.latitude; 
    float longitude_current = newLocation.coordinate.longitude; 
} 
0
-(void)postCurrentLocationOfDevice 
{ 

    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.distanceFilter = kCLDistanceFilterNone; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [self.locationManager startUpdatingLocation]; 
    self.locationManager.delegate = self; 

} 
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CURRENT_LOCATION = [locations objectAtIndex:0]; 
    [self.locationManager stopUpdatingLocation]; 

} 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CURRENT_LOCATION = newLocation; 
    [self.locationManager stopUpdatingLocation]; 
} 
Смежные вопросы