Getting User Current Location in Android using FusedLocatonProviderClient

Shoaib Kakal
3 min readJul 7, 2021
Photo from PubNub

Location API is one of the key important parts of Android development. In this article, we will build a simple application to get the user’s current location in a few steps.

First of all, we will be adding Play-Services-Location Dependency so add that and sync now. Right now 18.0.0 is the latest

//Location Dependency
implementation 'com.google.android.gms:play-services-location:18.0.0'

In case the dependency is updated in the future, Android Studio will prompt you to update it. Thanks to Android Studio Team :).

Once dependency is added, now we need to add Location Permission into the AndroidManifest.xml file.

Learn more about Permissions: Permissions on Android

Now we just have added the Permissions and told the android what permissions do our application requires to function But yet our application requires the user’s permission to fetch their location. So, you might have guessed the next step, yes we are going to prompt users to give us permission to fetch their location.

We’ll write a function name askPermissions().

In the below askPermissions(), we used simple logic.

if(!permission)
{
requestPermissions()
}
else{
//permission is already given fetchLocation(); //fuction to fetch location, once permitted.
}

onRequestPermissionResult(), is an override method so it plays its role once the user accepts or rejects permission.

Now if you call askPermissions() in your onCreate() and runs your application, you will see the following output:

You might see an error on fetchLocation() because we haven't declared it yet, so you can comment on it before running the applicaiton.

Picture from the Internet.

Right now we are dealing with ‘Allow Permission’ only to keep things simple and focused on fetching location.

You are amazing, we have done half of the job. let's complete it by calling FusedLocationProviderClient, this class is the main entry point for interacting with the location. Declare a lateint var in MainActivity.kt

Here comes the moment of truth, declare a new function to fetch the user’s Current Location. I’ll call it fetchLocation(), this method has with the help of fusedLocationProviderClient access the user's current Location including all other properties like altitude, provider, etc.

The above method is as simple as it should be and showing the outputs in Android Studio Logcat window.

Make sure your MainActivity.kt looks like this.

If you’re running this app on a real device make sure your location is already enabled, we didn’t handle that scenario where your location is not enabled, may be in next article.

Run the app and open logcat window.

Boom, you did it. we got latitude and Longitude respectively, in the next article we will convert this information into some meaningful real-time address string using Geocode API. Stay tuned.

World Health Organization says some nations aren’t running enough coronavirus tests: ‘Test every suspected case’

TAKE CARE, STAY HOME

--

--