Brandon
Brandon

Reputation: 2171

Waiting for Accurate didUpdateToLocation

I am trying to find accurate lat/lon coordinates for my user. I referred to the docs and found out about the LocateMe example project. I am trying to edit the code so that an alert pops up letting the user know if the location info is accurate.

I have the code below, but the alert is always saying that the user's location information is inaccurate ("Location Bad"). Does anyone know what Im doing wrong?

I just realized that the example code from Apple includes the line: [setupInfo setObject:[NSNumber numberWithDouble:kCLLocationAccuracyHundredMeters] forKey:kSetupInfoKeyAccuracy];

How would I edit my code below to wait for kCLLocationAccuracyNearestTenMeters. If possible, I would rather not have to refer to another view controller, but be able to include this directly in the method.

Any help would be wonderful. Thank you!

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

    //CAN I PUT THE DESIRED ACCURACY HERE?
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m

    [locationMeasurements addObject:newLocation];

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

    if (newLocation.horizontalAccuracy < 0) return;

    if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > 
    newLocation.horizontalAccuracy) {

        self.bestEffortAtLocation = newLocation;

        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Good" 
            message:@"OK" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, 
            nil];
            [alert show];
            return;      
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Bad" 
            message:@"OK" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, 
            nil];
            [alert show];
            return;
        }
    }
}

Upvotes: 0

Views: 266

Answers (1)

some_id
some_id

Reputation: 29886

Putting it in the delegate callback would be too late as the callback is already returning a result after using default desiredAccuracy.

You could place the setting in your init method and then call start on the location manager.

// set this before starting the calculations.
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
[self.locationManager startUpdatingLocation];

The only thing I find an issue with this is the placement of it within the UX flow as it will pop the alert up and if done at the wrong time from a UX perspective could lead to users disabling this feature. If it is fundamental to the app to use location services, well, it's not good. :)

Hope this helps.

Upvotes: 1

Related Questions