How to Implement Current Location Button Feature in Google Maps in Android?
The current location is a feature on Google Maps, that helps us locate the device’s position on the Map. Through this article, while we will be implementing Google Maps, we shall also be implementing a button, which will fetch our current location and navigate it on the map. Note that we are going to implement this project using the Kotlin language.
Follow the below steps to implement a map and a button to navigate to the device’s current position
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Get and hide the API key
Our application utilizes Google’s Places API to implement Google Map, so we need to get the Places API key from Google. To get an API key, please refer to Generating API Keys For Using Any Google APIs. Hiding an API key is essential and to do so, please refer to How to Hide API and Secret Keys in Android Studio?.
Step 3: Adding the dependencies in the build.gradle file
We need to add the below dependency for importing libraries to support the implementation of the Google Map.
Step 4: Adding permissions to the application in the Manifest.xml file
As the application majorly deals with the current location, we give the application the permissions to access the location. To give the application such permissions, declare permissions between the manifest and application opening tags in the following way.
Step 5: Implementing a simple Button & Google Map fragment in the activity_main.xml file (front-end)
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 6: Working with MainActivity.kt (back-end)
What we did in short is:
- Fetched the API key that we stored in Step 2.
- Initialized the Places API with the use of the API key.
- Initialized the Map fragment in the layout (activity_main.xml).
- Initialized fused location client.
- Initialized Button in the layout (activity_main.xml).
- Created a function to get the last location.
- Created a function to request a new location.
- Created a function for location callback.
- Created a function to check if the GPS of the device is turned on.
- Created a function to check if permissions for accessing the location are granted.
- Created a function to grant requests to permissions for accessing the location.
- Created a function to call getLastLocation() when permissions are granted.
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Note: The application will prompt for permission requests, kindly allow once. Also, keep the device connected to the internet.