Question

I'm working on an app where it checks your location every minute or so to see if your location changed. I am simulating the location changes by using a simulated location in xCode. First I'll do London, then when it updates that I'll change it to something like Tokyo, and then when it's time to check the location again it just won't update, it still thinks it's in London.

-(void)recheckLocation{
    locationManager = nil;

    [recheckLocation invalidate];

    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    geocoder = [[CLGeocoder alloc] init];

    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {

            placemark = [placemarks objectAtIndex:0];
            NSString *Location = [NSString stringWithFormat:@"%@, %@",placemark.locality, placemark.administrativeArea];

            recheckLocation = [NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:@selector(recheckLocation) userInfo:nil repeats:YES]; 

        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

No matter how many times I changed the location it just keeps giving me the same location every time rather than the correct one. Can someone tell me what I am doing wrong?

Was it helpful?

Solution

Its bit older post and I am having basic knowledge in CLLocationManager stuff, but still I am trying to address the problem. I understand from your above code that CLLocationManager is recreated everytime. CLLocationManager is like a singleton class you don't need to recreate everytime. You just need to call [locationManager startUpdatingLocation]; .

Also everytime you are calling [geocoder reverseGeocodeLocation:xxx]. Probably you need to find accuracy before you do this so that it will not get called again and again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top