1

This is the Code I use to get the current location:

mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
if (best == null) {
    //ask user to enable atleast one of the Location Providers
} else {
    Location location = mgr.getLastKnownLocation(best);
//But sometimes it returns null
}

Almost everytime best = network

But it's not providing the location sometimes.

mgr.getLastKnownLocation(best) returns null

Also:

onResume() {
    mgr.requestLocationUpdates(best, 15000, 10, this);
}

and

onPause() {
    mgr.removeUpdates(this);
}

Is there any alternate for such cases?

One option might be

List<String> providers = mgr.getAllProviders();

Store all the providers and go 1 by 1. But never saw this recommended anywhere. Moreover asking for the best provider is what the docs suggest.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • 1
    "But its not providing the location somethimes" -- if nothing has tried using that provider recently, it will not have a location to give you. You may wish to read the docmentation: http://developer.android.com/guide/topics/location/strategies.html – CommonsWare Mar 14 '13 at 12:51
  • `if nothing has tried using that provider recently`--How will something use the provider for the first time. Not using `getLastKnownLocation(provider)`? – Archie.bpgc Mar 14 '13 at 12:55
  • Quoting the documentation that I linked to: "Getting user location in Android works by means of callback. You indicate that you'd like to receive location updates from the LocationManager ("Location Manager") by calling requestLocationUpdates(), passing it a LocationListener." – CommonsWare Mar 14 '13 at 12:56
  • Yeah I am using `LocationListener`. The class above `implements LocationListener`and required call backs are implemented. – Archie.bpgc Mar 14 '13 at 12:59

1 Answers1

7

getLastKnownLocation() only returns a Location object for a provider if that provider has been used recently. If it has not been recently, Android assumes that whatever was the last location given by that provider is out of date and wrong, and returns null.

In such a case, you will have to wait for your LocationListener's onLocationChanged() method to be called before you have a usable location.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • And when will `onLocationChanged()` be called. Because if its dependent on device movement, It will always return `null` no matter how many times I try, unless the user moves? – Archie.bpgc Mar 14 '13 at 13:12
  • It should be called whenever there is a location that is different from the last known location. So in this case since you get null for that, onLocationChanged() should be called at least once when the provider gets an initial fix. This can range from a few seconds to a few minutes. – Raghav Sood Mar 14 '13 at 13:16
  • Okay. 1 last question. The device I am currently testing on has never went out of this city. All these days it gave the right current location. But yesterday it gave a lat lng with difference 5. Which is a place 1200 kms away from my current location. Any idea what might have caused it? – Archie.bpgc Mar 14 '13 at 18:25
  • do you have any official docs/links for the behavior of `getLastKnownLocation()` ? the javadoc is very err slim - see also discussion [here](http://stackoverflow.com/questions/2250597/find-current-location-latitude-and-longitude#comment25840492_2251680) – Mr_and_Mrs_D Jul 18 '13 at 16:33
  • 1
    It would be very nice if the documentation actually described this behaviour; the documentation states that it returns a location that may be out-of-date, or null if the provider is disabled. – Jules Aug 12 '13 at 09:40