Geolocation - Get current location of android phone

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I need to get the coordinates of the user when my app starts, and every solution I have found involved setting up a LocationListener and implementing

@Override
public void onLocationChanged(Location location) {
    // code
}

This is slow, it takes about 5 seconds, sometimes more to get the location which is unacceptable to me. I do not need to know when the location changes, only where the user is when the app is started.

I m guessing this can be done, since when I open(in another app) the googleMaps fragment, it automatically knows where I am and it sets the position there instantly.

Is there a way to get the last known coordinates of the phone, and if so, how accurate is this data?

Answers

The LocationManager has a method to get the last known location. http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29 http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29

The location returned here might be old though, so I recommend you to use this as a first result and then update it if you get the OnLocationChanged call

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/32220812/get-current-location-of-android-phone

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils