CLLocationManager need to call twice

Discussion in 'iPhone/iPad Programming' started by walty, Nov 12, 2008.

  1. macrumors member

    Joined:
    Jul 15, 2008
    #1
    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.

    Code:
    	[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,
    Any advice for this please? Thanks a lot.
     
  2. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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.
     
  3. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #3
    location updates are asynchronous - you shouldn't really call stop until you have received what you need.

    See the LocateMe app for an example.
     
  4. thread starter macrumors member

    Joined:
    Jul 15, 2008
    #4
    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.
     
  5. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    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.


    .
     
  6. macrumors newbie

    Joined:
    Oct 5, 2008
    #6
    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.
     
  7. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #7
    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 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.
     
  8. thread starter macrumors member

    Joined:
    Jul 15, 2008
    #8
    ok, got it. Thanks a lot everyone :)
     

Share This Page