I just need to get the location of the user. An exact location is preferred, but if this is not possible, an approximate location will be good.
According to the docs:
LocationClient.getLastLocation ()
Returns the best most recent location currently available.
and
LocationManager.getLastKnownLocation (String)
Returns a Location indicating the data from the last known location fix obtained from the given provider.
If my understanding is correct, the former will give me a very good result (or null sometimes), while the latter will give me a result that would rarely be null.
This is my code (simplified)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationClient = new LocationClient(this, this, this); @Override public void onConnected(Bundle dataBundle) { setUserLocation(); } private void setUserLocation() { myLocation = locationClient.getLastLocation(); if (myLocation == null) { myLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false)); if (myLocation == null) {
It seems like I'm working, but my questions are:
- Is my understanding of
getLastLocation and getLastKnownLocation ? - Is this a good approach?
- Can I get into a problem using both in one activity?
thanks
android location
Androiderson
source share