How to get the current location latitude and longitude in android

If you are building a location based or map based application in android ,it is important to know the current location of the android device. For example application like Uber ,ola, etc will always fetch the current latitude and longitude to display the cab location of the drivers.

In android we can get the location of the users device using location manager either by network provider or by GPS provider. In this tutorial we will learn how to fetch the current latitude and longitude of the android device.

Contents

  1. Adding necessary permission to the AndroidManifest.xml file
  2. Learn the code for getting location using network provider and GPS
  3. How to fetch the latitude and longitude from the location class
  4. How to get the location name from latitude and longitude

Adding necessary permission to the AndroidManifest.xml file

The first thing is you need to have the following permission in your AndroidManifest.xml. Without this permission you will not be able to access the location of the android device. Please note that if your target api is greater than 21 you need to request and get the permission dynamically . If you are not familiar with  requesting permission dynamically in android please refer android request permission dynamically example

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

Learn the code for getting location using network provider and GPS

Now we need to check whether the android device is enabled location services . For that we need to check either GPS or NETWORK_PROVIDER is enabled or not, Implement the following function in your activity which will check whether the location service is enabled , If the location service is enabled it will fetch the location using LocationManager and the function will return location. If location service is not enabled it will take you to the settings screen to enable location services.

public Location getLocation() {

Location location = null;
final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

try {
LocationManager locationManager = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//updating when location is changed
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
};

boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

if (!isNetworkEnabled && !isGPSEnabled) {
//call getLocation again in onResume method in this case
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

} else {

if (isNetworkEnabled) {

locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

} else {

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);

}

}


}

} catch (Exception e) {
e.printStackTrace();
}

} else {

//Request for the ACCESS FINE LOCATION
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
//call the getLocation funtion again on permission granted callback
}
return location;
}

How to fetch the latitude and longitude from the location class

Now use the following code when you want to fetch the latitude and longitude anywhere in activity.

//Getting current latitude and longitude using network provider
Location location = getLocation();
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}

How to get the location name from latitude and longitude

The above double values latitude and longitude will contain the current latitude and longitude of the device location. Use the below code if you want the Location name

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String city= addresses.get(0).getSubLocality();

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

4 Comments

  1. Hari prasad May 25, 2018 Reply
  2. angelina May 17, 2018 Reply
    • Ramees FazalAuthor May 18, 2018 Reply
      • Hari prasad May 24, 2018 Reply

Add a Comment

Your email address will not be published. Required fields are marked *