Android Current Location On Google Map Marker Example Tutorial

We will develop Android Current Location On Google Map with marker in this example.

Now a days, number of people using google map is increasing significantly.

So most apps are using google map to simplify the user experience.

This tutorial will guide you to show current location on google map with marker in android studio.

First we will fetch the current latitude and longitude and then we will use them to show current location marker on google map.

For getting current location, we will use google’s FusedLocationAPI.

We will update the current location marker constantly on the google map with the current movement of the user.

Prerequisite For This Tutorial

To go ahead with this example, you need to know some important details regarding google map and API Key.

First of all read this tutorial to get google API Key to integrate google map in android

You should create a project in google developer console and should get API Key first using above tutorial.

Do not create android studio project from above example. Just get your Google map API Key and after getting API Key, follow all the below steps.

Step 1. Opening Maps Activity

Create a new project in android studio. Give the name, directory, sdk level etc. details of new project.

After this, you will have a screen like the below image.

google map integration in android, android current location on google map

Select Google Map Activity

  • You need to select a default activity for your new project. We generally select Empty activity which have plain workspace.
  • But in this case, select Google Maps Activity as it will integrate google map automatically in the default activity.
  • You just need to update your API Key So, it will simplify our process and will save our lot of time.

Step 2. Change gradle file

Now it is time to do some changes in build.gradle(Module: app) file

By default, you will have the below line in your build.gradle(Module: app) file because of Google Maps Activity

We also require following two lines here

So final code for build.gradle(Module: app) file is as below

Step 3. Writing GOOGLE API Key

There is a file named google_maps_api.xml which is system generated.

  • This google_maps_api.xml file is available at res->values directory.
  • You need to write your Google API Key here in this file.

Code for this file is as following

  • Add your API key in between <string> Your API Key</string> tags.

Step 4. Updating Manifest file

By default, you will have ACCESS_COARSE_LOCATION in your AndroidManifest.xml file

  • Replace it with ACCESS_FINE_LOCATION because it is required to fetch more accurate current location.

Final code for AndroidManifest.xml

Step 5. Final Changes

Now we need to change the code of activity_maps.xml and MapsActivity.java file

Replace your existing code of activity_maps.xml with the following one

  • Here I have taken four textviews which will represent the latitude and longitude values.
  • One fragment is also there, which will open a google map.
  • You will also some pre-written code in MapsActivity.java file.

Replace it with the below source code

Explanation of Maps Activity

Following snippet defines necessary variables.

  • As you can see that GoogleMap, LocationRequest, LocationManager, Location etc. class’s objects are defined in the above code.
  • long variables UPDATE_INTERVAL and FASTEST_INTERVAL defines the specific time amount.

Now consider the following source code

  • First of all, compiler will check if the location permissions is granted or not. It will use requestSinglePermission() method for this purpose.
  • requestSinglePermission() method will return a boolean variable (true or false) . If the user grants the location permission then it will return true otherwise false.
  • If requestSinglePermission() return true, then compiler will start the process to fetch the current location and then it will create google map with the help of that current location.

Code snippet for requestSinglePermission() method is as below

  • We have used Dexter library in this method. We will request for ACCESS_FINE_LOCATION in this method.

Now look at the below method

  • This method is called when google map is ready to load.
  • You need to provide location in terms of latitude and longitude in this method. Map will move it’s camera at this location.
  • Marker is also added in this method only.
  • onConnected(), onConnectionSuspended(), onConnectionFailed(), and onLocationChanged() methods are called by the compiler while fetching the current location.

Compiler will get the current latitude and longitude in onLocationChanged() method.

Source Code for onLocationChanged() method is as following

  • Compiler will call this method every 20 seconds. After getting the location, compiler will again navigate the camera to the new location.
  • Hence, Google map will be updated every 20 seconds as per the current movement of the user.

End for this tutorial. Keep navigating over DemoNuts to enhance your knowledge.

Google Maps Tutorials

Following are other useful tutorials on Google Map.

Last Image

After successful implementation of all the above steps, you should get the following output

I am living in Rajkot, India so red marker is there.

android current location on google map

Download Source Code For Android Current Location On Google Map

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *