Is there a way to obtain information without displaying the map? I want get my coordinate of my place during the work with other activity. tnx a lot.
-
yes, you dont have to use a map to get the longitude and latitude – JRowan Dec 15 '13 at 23:10
-
same question here with anwser http://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android – TootsieRockNRoll Dec 15 '13 at 23:13
-
Link Mentioned above gives the best answer. Just adding to it, based on your requirement. you have to choose how many minutes once, you need the location information. Reason is listeners drain device battery more. So, choose a scenario appropriately. – Prem Dec 15 '13 at 23:21
-
http://stackoverflow.com/questions/20177989/locationmanager-dont-get-the-right-results/20179234#20179234 – Naddy Dec 16 '13 at 03:54
3 Answers
By Using LocationManager
you can get lat and lang :
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 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.

- 20,649
- 15
- 100
- 138
Using this code able to get latitude and Longitude values and make sure before using application you got permission from manifest for accessing location
if(isGPS) {
Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
try
{
double lat=location.getLatitude();
double lng=location.getLongitude();
Toast.makeText(getApplicationContext(),lat+"and"+lng, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
display("GPS not Enabled");
}
Manifest permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

- 3,092
- 2
- 21
- 33
Use this if you want to get lat lon without internet
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitude = location.getLongitude();
longitude = location.getLatitude();

- 1
- 1
- 17
- 28