views:

2521

answers:

3

How to get the current Latitude and Longitude of the mobile device in android?

+14  A: 

Use the LocationManager.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.

private final LocationListener  = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);

You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

Dave Webb
Thank you very much it's working
deepthi
A: 

From reading the platform documentation: http://developer.android.com/guide/topics/location/index.html

Matthias
please no rtfm answers... I use stackoverflow for things that I know are in the documentatin but here they are very often much much clearer and I find them faster
Janusz
Then you should formulate your question in a way that doesn't sound as if you didn't bother to even think one single second about it. For instance, you could have said: "I've read the documentation over at ... but I don't quite understand X and how can I achieve Y"? I certainly won't bother spending my time writing in-depth answers to someone who isn't even willing to ask a proper question.
Matthias
Don't be a dick. The docs aren't nearly this concise.
greg7gkb
A: 

will you please share complete source code?

Bill
Bill - this isn't an answer.
Mike Hodnick