iosobjective-ccllocationmanagercllocationclregion

CLLocationManager always returns the exact same location


I am trying to set up a new CLLocation and CLRegion, but the location always returns with the same coordinates, regardless of where I actually am. My main UIViewController conforms to the CLLocationManagerDelegate protocol, as does a custom class. When the custom class initializes, I have a timer set to fire .25 seconds later, with this code:

mainLocationManager = [[CLLocationManager alloc] init];
[mainLocationManager startUpdatingLocation];
location = [mainLocationManager location];
if (![location.timestamp isEqualToDate:[NSDate dateWithTimeIntervalSinceNow:0]]) { //filters cached data by updating location if cached data not recent enough
    location = [mainLocationManager location];
}

//Set up region
region = [[CLRegion alloc] initCircularRegionWithCenter:location.coordinate
                                                     radius:500
                                                 identifier:[NSString stringWithFormat:@"region:%@", locationName]];
NSLog(@"%@       %@", location, region);

The code appears to work except that the coordinate is always the same value. I am not sure how to change this. Any help is appreciated, thanks!


Solution

  • I think because you did not update location by location manager delegate method.

    Get update location by this method

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    
    {
    location_updated = [locations lastObject];    
    NSLog(@"updated coordinate are %@",location_updated);
    
    }
    

    Also test it in device.