How do I get the current Latitude and Longitude of the mobile device in android using location tools?
Android: Open Google Maps; it will zoom to your approximate location. Press and hold on the screen to drop a pin marker. Click on the dropped pin; latitude and longitude will be displayed below the map.
How to get current location latitude and longitude in Android? This example demonstrates how do I get current location latitude and longitude in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
– You will start on the MainActivity which is the activity that app will launch initially. – Touch/Click on to ‘Start’ button which will take you to the GPS activity. – After 7-8 seconds you will see Latitude and Longitude on the EditText fields.
There are a number of ways to find your GPS location on your Android device. Unlike the iPhone, the Android system doesn’t have a default, built-in GPS coordinate utility that shows you the information the phone already has. You have to find an Android app that can provide this functionality.
Unlike the iPhone, the Android system doesn’t have a default, built-in GPS coordinate utility that shows you the information the phone already has. You have to find an Android app that can provide this functionality.
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 locationListener = new LocationListener() { public void onLocationChanged(Location location) { longitude = location.getLongitude(); latitude = location.getLatitude(); } } lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With