Best way to get user’s location in android app using Location Listener from JAVA in android studio

Rudransh Gupta
3 min readMay 17, 2020

--

In this post, we will see how to get latitude and longitude of a user in Android app using java programming(Android Studio). And hence, get user’s current address(district, state, postal code, country etc).

Recently I made my first android project- Corona Virus live location Status. It tells the current cases of Corona Virus around user’s location. Then it tracks cases in user’s district using live API. The biggest problem I had to go through, is getting device’s live location in android app. I believe anyone could face these problems. After searching so many websites, I finally concluded the best way to locate the user.

Firstly, the most important task is to ask for permissions from your device. Believe me it does not create problems in any way. You just have to write these 2 lines of code in the onCreate() method and app will automatically ask for permissions from the device just after the activity is opened.

Asking for Android Location permissions in onCreate() method

The solution given in most websites including stack overflow, tutorialspoint is to use Location Manager and getLastKnownLocation() method. I used it almost 5–6 different methods and each time I somehow managed to get user’s location in Android studio. Their are several tricks(like opening google maps and locating self location to save last location and thus getLastKnownLocation() would work). But it does not work every time as setting last location is something the app should do it by itself. This method returns null location 90% of the times as the location will not be set always.

Using getLastKnownLocation() method to get user’s location

But as I said, this method fails most of the times. Hence, the best way I found to get user’s current latitude and longitude is Location Listener and get Location updates if the above method return null location. In your activity.java file you have to implement LocationListener method in seperate class and get updates as you click the button and then do the remaining taks.
LocationListener forces the device to get the current location. It should be used one time when the getLastKnownLocation returns null. After first time, the last location will be saved and getLastKnownLocation will work fine and fast. Thus, using the combination of getLastKnownLocation and Location Listener is the best way to achieve user’s current location.

LocationListener class

Above is the Location Listener class that needs to be added in activity.java file, inside the parent class and below the onCreate method. Thus when you want to print the location, calling onLocationChanged() will do the same.
Now coming to the implementation of complete process, first calling getLastKnownLocation will return null, thus we will make an instance of LocationListener class and ask for user to open the GPS in mobile. After opening GPS, we get back to the app and it starts getting device’s location. Usually, it takes about 3–5 seconds to get location(You can use timer here till it is getting location). After that, the device’s location will be shown. By requesting location update method, you can set the minimum conditions(distance and time) for a change to be shown in location

Criteria can be used to find the best location providers (read about it from internet). Also now that we got the location, inbuilt Geocoder library can be used to get the address from location and thus, city, state, district, Postal Code from the address. Given below is the code for complete implementation :

Thanks to all for reading it out. If you found it helpful, please comment below.

--

--