How to get user location in Android
Many apps in Android uses user’s locations be it for ordering cabs or delivering food and items. Here, a simple android app that would return the user’s latitude and longitude is made. Once the latitude and longitude are known, the exact location on Google Maps can be seen using the following query: https://www.google.com/maps/search/?api=1&query=,
Note: The app will run completely fine on an actual device but might show an error on an emulator. So, please try to run it on an actual device!
Step 1. Acquiring Permissions
Since using the user’s permission is a matter concerned with high privacy, first acquire the user’s permission to use their location by requesting them for it. From Android 6.0(Marshmallow), the concept of run-time permissions was rolled in and so the same will be used for getting permission. Any of the following permissions can be used for this:
ACCESS_COARSE_LOCATION: It provides location accuracy within a city block.
ACCESS_FINE_LOCATION: It provides a more accurate location. To do this, it needs to do some heavy lifting so it’s recommended to use this only when we need an accurate location.
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
In case the app needs to access the user’s location while the app is running in the background, we need to add the following permission along with the above ones:
<uses-permission android:name=”android.permission.ACCESS_BACKGROUND_LOCATION” />
We need to add all these permissions in the AndroidManifest.xml. To access this file, select your project view as Android and click on:
After adding all the permissions, this is how the AndroidManifest.xml file looks like:
Also, as Google’s PlayServices will be used to access the device’s location, add it in dependencies, in the Build.Gradle (app) file:
Step 2. Designing the layout
As the app is fairly simple, it would contain only the MainActivity and hence a single main layout. In the layout, add an ImageView and two TextViews which would be displaying the user’s latitude and longitude. The latitude and longitude which would be displayed will be returned from the logic of our MainActivity which will be discussed next. Here’s how activity_main.xml looks like:
Step 3. Writing the logic
- To work on the main logic of our app, we will follow the following key points:
- Check if the permissions we request are enabled.
- Else request the permissions.
- If permissions are accepted and the location is enabled, get the last location of the user.
- In order to get the last location of the user, make use of the Java public class FusedLocationProviderClient. It is actually a location service that combines GPS location and network location to achieve a balance between battery consumption and accuracy. GPS location is used to provide accuracy and network location is used to get location when the user is indoors.
- In conjunction with FusedLocationProviderClient, LocationRequest public class is used to get the last known location. On this LocationRequest object, set a variety of methods such as set the priority of how accurate the location to be or in how many intervals, request of the location is to be made.
- If very high accuracy is required, use PRIORITY_HIGH_ACCURACY as an argument to the setPriority(int) method. For a city level accuracy(low accuracy), use PRIORITY_LOW_POWER.
- Once the LocationRequest object is ready, set it on the FusedLocationProviderClient object to get the final location.
Let’s now look at the MainActivity.java file.
Note: In order to see this location in Google Maps, just formulate the query as:
This will look like this: