Android show current location in Google Map using Google Maps Android API

Day by  day our usability and dependency is increasing on Google Map. Whether it is location based App or ride hailing App it is integrated in every App.  In this tutorial we will learn about how to show current location in Google Map.

1. Create a Project: Create an Android project and choose Google Maps Activity and create this Activity. Now we need an API Key to access the Google Map in our App. So we need to create an API key for our App.

2. Create an API Key: First of all we have to create an API key for accessing Google map. Go to Google APIs Dashboard if you have existing project you can choose that otherwise you can create a new project for Google map. Now Follow the steps:

  1. Select Maps Android API and enable it.
  2. Back to the dashboard  and Click on credentials Now click on create credentials and choose API Key .
  3. Copy the API key.
  4. We can also restrict our API key. For this we have to add package name of our project and SHA1 key.
  5. Go to your Android Project and click on Gradle icon which is in the right side .
  6. Click on YourProjectName(root) after that if nothing is showing click on refresh icon.
  7. Now go to Tasks -> android -> signingReport. You will find SHA1 key. If you find difficulty please check step number 2 of Youtube channel integration in Android using YoutubePlayerView API. Here is the screenshot of siginingReport .

    show current location in Google Map

    SHA1 Key

3. Add API key in Android project:- Now go to google_maps_api.xml file of values folder and paste API key in string value.

4. Permission : Make sure location and internet permission is given in AndroidManifest file. If its not given add the permission.

5. Add Google Play Services: To show current location in Google Map we need to add Google Play services in build.gradle file of App.

6. Run the Project: Running the project we will find the default location of Google Map. We can also change the default location by changing latitude and longitude.

show current location in Google Map

Default location in Google Map

6. Layout File: We are using a map fragment in our layout file which is given below.

7. Important Points:

OnMapReadyCallback: This is a public interface. Once an instance of this interface is set on a MapFragment or MapView object, the onMapReady(GoogleMap) method is triggered when the map is ready to be used and provides a non-null instance of GoogleMap.

GoogleApiClient.ConnectionCallbacks:  Provides callbacks that are called when the client is connected or disconnected from the service.

GoogleApiClient.OnConnectionFailedListener: It provides callback in case of connection failed and attempt to connect with client to the service.

LocationListener: Whenever the user changes location this callback is called. We use onLocationChanged() method to get Latitude and Longitude of a location.

 

8.  CurrentLocationMap.java:  This is our full code of after implementation of  all required methods.

9. Demo: 

 

 

This is all about to show current location in Google Map. You can also check other tutorials as well.

Youtube channel integration in Android App

Android Login and Signup with PHP MySQL

Parse JSON data into Spinner in Android