1

In iOS with the CoreLocation framework (for GPS tracking), how do I ask for the location instead of getting continuous updates. I don't want didUpdateToLocation to be called every second or so, I just want to get the location when I need it.

Thanks!

Michael Eilers Smith
  • 8,466
  • 20
  • 71
  • 106

5 Answers5

2

I don't believe there is a way to do this. You could just start the updates then in didUpdateToLocation disable them so you only get one.

ACBurk
  • 4,418
  • 4
  • 34
  • 50
1

Stop the location service when you got the location successfully

NeverBe
  • 5,213
  • 2
  • 25
  • 39
1

You can't. The hardware (the radios, etc.) isn't fast enough. Instead, you have to call the startUpdatingLocation method a bit before you need a location, and wait for the radios to get as accurate a fix as you need (the very first few location callbacks might not be very accurate or as accurate as later callbacks). Then you can stop location updates if you don't need them for some long period of time to help save battery life.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

As I suggested in a comment, you could use a combination of -startUpdatingLocation and -stopUpdatingLocation at will, if you stop the updating upon receiving a location update it will not continue unless you start it again. Example below:

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

    //Store your new location before stopping updates.

    [manager stopUpdatingLocation];
}

Now, when you want to retrieve a new location at some other time in the app. Without worrying about how often it updates, since it will only be once.

[locationManager startUpdatingLocation];

Hope this makes sense, and helps !

skram
  • 5,314
  • 1
  • 22
  • 26
1

When you call startUpdatingLocation method, it will return cached point first. So, in the delegate method didUpdateToLocation, you should check for accuracy of that location. If it is not with in acceptable range, you should ignore that location. You should continue getting location till the point falls with in acceptable range. Once such point is received, you should call stopUpdatingLocation.

Apurv
  • 17,116
  • 8
  • 51
  • 67
  • How can you check the accurary of that location? – Michael Eilers Smith Jun 10 '12 at 01:40
  • Refer http://stackoverflow.com/questions/4635938/cllocationmanager-and-accuracy-issues-any-experiences – Apurv Jun 10 '12 at 05:53
  • Check the age, not the accuracy. It might be accurate but old (cached). – progrmr Jun 14 '12 at 01:48
  • So, you should return from the function without calling stopUpdatingLocation. Once you receive the location check below, location.horizontalAccuracy > 0 && location.horizontalAccuracy < desired accuracy(100 meter). If it satisfies, call stopUpdatingLocation and save this location as your current location. – Apurv Jun 14 '12 at 02:27