kimv
kimv

Reputation: 1579

Get current location of android phone

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?

Upvotes: 1

Views: 131

Answers (1)

F43nd1r
F43nd1r

Reputation: 7749

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

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

Upvotes: 1

Related Questions