Question

I am using core location to store lat/long coordinates. I have the following code in viewDidLoad.

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

Here is my delegate method.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    [self.locationManager stopUpdatingLocation];
    CLLocation* loc = [locations lastObject]; // locations is guaranteed to have at least one object
    float latitude = loc.coordinate.latitude;
    float longitude = loc.coordinate.longitude;
    self.lat = [NSString stringWithFormat:@"%.8f",latitude];
    self.longString = [NSString stringWithFormat:@"%.8f",longitude];
}

I'm using GPS to track these coordinates and it seems inconsistent. Upon launching the app, the location services icon does not show up often. However, if I toggle my wifi, it'll work. Or, if I open the Maps app and prompt it to obtain my current location, and then switch to my app, it'll work. Sometimes, it'll just work irregardless. If the location services icon doesn't show up then my lat/long values are null. Any ideas? Thanks in advance!

Was it helpful?

Solution

The problem is that you are immediately stopping updating locations when the very first update arrives. That's like flicking a light switch on and off so fast that there's no time for the light bulb to start glowing. It takes time and several updates for all the sensors to warm up and get a decent fix, especially the GPS. You need to allow enough updates to arrive until you do in fact have a location (and you probably want to check the accuracy to make sure it is a reasonably accurate location).

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