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
2010-02-09 06:49:40
Thank you very much it's working
deepthi
2010-02-16 10:47:15
A:
From reading the platform documentation: http://developer.android.com/guide/topics/location/index.html
Matthias
2010-02-09 12:35:31
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
2010-03-05 14:30:27
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
2010-03-05 16:49:32