0

Right now, I have it so when a button is clicked, it starts location services, sets the latitude/longitude variables. But location still continues to be running in the background.

I want is so when I click a button it finds the latitude/longitude, all location services stop.

Here's what I have currently.

- (IBAction)startButton:(id)sender {

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    NSLog(@"%@", [self lat]);
}

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

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

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

Another problem is, if I instantly try to retrieve the value of the lat property, it returns null, most likely because it takes a second or two to find the location.

So I would also need to stop any further code to be run until the lat/long properties are set. For example, How would I stop the otherFunction from running until the latitude/longitude are found:

- (IBAction)startButton:(id)sender {

    [self updateLocation];
    [self otherFunction];
}

- (void)updateLocation
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

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

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

    if (currentLocation != nil) {
        [self setLat:[NSString stringWithFormat:@"%.6f", currentLocation.coordinate.latitude]];
[locationManager stopUpdatingLocation]
    }
}

Any help/ideas would be appreciated!

Bobby W
  • 836
  • 8
  • 22
  • 1
    Calling `[locationManager stopUpdatingLocation]` should stop it. You may check `lat` value (and `longitude`) to know when stop it. What have you tried? – Larme Aug 18 '14 at 17:41
  • @Larme: Yes that works, but in regards to my second piece of code, how can I prevent `[self otherFunction]` from running before lat/long was set? – Bobby W Aug 18 '14 at 17:47
  • 1
    Call otherFunction when you get the location (ie, just after you stopUpdatingLocation. – Larme Aug 18 '14 at 17:48
  • @Larme: otherFunction must be triggered by the button. Edit: Maybe not, might structure this differently – Bobby W Aug 18 '14 at 17:49

1 Answers1

0

In your last function, after you verify that the currentLocation is not nil, then you should check for the accuracy of currentLocation, then call your setLat: function, then stop updating the location. See this post for more details:

CLLocationManager and accuracy issues - any experiences?

Community
  • 1
  • 1
Gazzini
  • 728
  • 8
  • 19