PDA

View Full Version : CLLocationManager need to call twice




walty
Nov 12, 2008, 10:59 PM
Hi,

When I try to get current location (single spot only, not continuous), I found that a single call of following only return (0,0) coordinate. I have tried to use a "for" loop with some "sleep()" inside to get the location, but all failed.


[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
[locationManager startUpdatingLocation];


But call of same code in the succeeding events get the current location correctly.

It seems to be that the GPS location collection is available only when triggered in more than one event, which is quite weired..

But according to SDK,

Calling stopUpdatingLocation in between, however, does cause a new initial event to be sent the next time you call this method.


Any advice for this please? Thanks a lot.



firewood
Nov 13, 2008, 12:28 AM
If you ask for a location between measurements, it will give you a previous measurement. They're time stamped so that you can ignore any really old ones. Then wait for the next measurement with a more recent time stamp.

jeremy.king
Nov 13, 2008, 10:40 AM
location updates are asynchronous - you shouldn't really call stop until you have received what you need.

See the LocateMe app (http://developer.apple.com/iphone/library/samplecode/LocateMe/index.html) for an example.

walty
Nov 14, 2008, 12:25 AM
But according to SDK, calling stop could actually force the collection of current GPS data :confused:

And the problem is, I don't want the "GPS Location Prompt" to be triggered until the user really need it. So I don't want to call the location start at the begin of program. Or maybe this is inevitable actually?

Thanks.

firewood
Nov 14, 2008, 01:24 AM
The GPS radio takes some amount time to find its location (my old dedicated handheld GPS would take 3 or 4 minutes to figure out which satellites to look at). The AGPS is a little faster, but nowhere near instant. If you don't start it until you need it, it will be too late, and you may end up with an old stale leftover location.


.

xnakx
Nov 14, 2008, 01:26 AM
you got it right.
CoreLocation will always first return the last location it got.
best to ignore the first and wait for other updates.
in each callback from CLocationManager I ++ and int and wait for the second or third one.

jeremy.king
Nov 14, 2008, 10:16 AM
But according to SDK, calling stop could actually force the collection of current GPS data :confused:

It returns the last known location, which can be quite different than current position. Calling start and then stop immediately doesn't give the CLLocationManager enough time to determine the current location. As firewood mentioned, the returned CLLocation (http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html) has a timestamp property and depending on your accuracy needed, an older location may suffice. This is up to you, the developer, to decide. Otherwise, wait for an update event with your delegate and then call stop.

walty
Nov 15, 2008, 12:07 AM
ok, got it. Thanks a lot everyone :)