17

So I'm trying to sample the gps coordinates just once in an application. I don't want to create a LocationListener object to constantly get gps updates. I want to wait until receiving the coordinates, and then proceed on to another task.

Here is a code snippet

LocationManager lm = (LocationManager)act.getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(crit, true);
Location loc = lm.getLastKnownLocation(provider);

The loc variable is always null in the emulator. I tried using the command "geo fix latitude longitude" to set it, and also I tried using the DDMS way of setting it. Neither method had any effect on the code. Also the snippet isn't causing any exceptions.

Thanks for your help.

Robert Parker
  • 1,145
  • 5
  • 12
  • 14
  • 1
    Here's a nice example with source code [http://marakana.com/forums/android/android_examples/42.html](http://marakana.com/forums/android/android_examples/42.html) – Fedor Dec 01 '09 at 13:46

2 Answers2

12

The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.

A possible check could be to see if the settings in it disable gps provider ? then send geo fix.

However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.

Note: It can take some time on device to get current location, and even on device this can return null.

krishc
  • 368
  • 1
  • 9
  • Yeah, doing a LocationListener then waiting till you have a fix you're happy with is pretty much your only option. As mentioned, you can immediately unregister it. – Jeremy Logan Oct 23 '09 at 07:15
  • 8
    It turns out that without any LocationListener it will not work. Even just adding a mock LocationListener that doesn't even do anything will cause the phone to get gps coordinates. – Robert Parker Nov 02 '09 at 22:55
  • I am assuming LocationListener updates some cached location, which locationManager uses. Thanks for posting. – krishc Nov 04 '09 at 21:24
  • you can also aimed for having a broadcast receiver and request the update with a PendingIntent – Necronet Apr 11 '13 at 16:09
  • 5
    So if the method is called last known location, why cannot it return the last known location rather than waiting for a new fix. Seems very counter productive to me. – Andrew S May 07 '13 at 19:07
0

Try using the MyLocationOverlay , create a runnable that does what you need to do with that GPS location, and pass it to

boolean     runOnFirstFix(java.lang.Runnable runnable)
          Queues a runnable to be executed as soon as we have a location fix.

and then disable the location updates for the MyLocationOverlay.

Edit: The reason the location is null is because at the time that code is run, no geofix has been received.

I82Much
  • 26,901
  • 13
  • 88
  • 119
  • I get a null too, but the Google maps app know's where I am instantly so there must be some fix stored away somwwhere. – Andrew S May 07 '13 at 19:09
  • Using a runnable is clever but there's no need to bring in the google maps api for this problem. – Dorje Sep 03 '13 at 05:03