Android Google Map Draw Path Between Current Location And Destination

Android Google Map Draw Path Between Current Location And Destination in this tutorial.

At the end of this tutorial, you will learn how to draw path as i move starting from my current location using google maps.

First of all, we will fetch the current location in terms of latitude and longitude of the user.

Then using this current location and destination coordinates, we will draw a polyline between these two points on android google map.

Following is the output of this example.

Get your Google API

In order to load google map in android, we need to generate one API key in google developer console.

For this purpose, read “Work At Google Developer Console” part in Google Map Android Tutorial. carefully and follow all it’s steps.

Once you have done above step, you will have one Google API key, which we will add in android studio project in few minutes.

After creating API key, we need to enable the Directions API.

Now, watch the following image

android google map draw path, android google map draw path between current location and destination

At google developer console You can see a navigation icon at upper right corner as per above image. Click on it.

A slider menu will be opened, like the following picture

android google map draw path, android google map draw path between current location and destination

Sliding menu

As per the image, click on APIs option.

System will open the screen like below picture

android google map draw path, android google map draw path between current location and destination

Direction API

When you click on Directions API, system will open the following window

android google map draw path


Click on the ENABLE button as per above image.

Make sure that you have enabled Direction API otherwise you will not be able to draw a path between two markers.

Now, we have Google API key and we have enable Directions API.

Work At Android Studio

In the android studio, make a fresh new project.

While creating new project, select “Empty Activity” as a default template.

Do not choose Maps Activity at all.

Now go through all the below steps.

Step 1. Adding API key

Time to write Google API key which you have already generated.

Inside res->values->strings.xml file, add the following line

Replace “YOUR_GOOGLE_API_KEY” with your original API which you have already generated.

Step 2. Gradle dependency and Manifest changes

We need to add some dependencies that will help us to reduce complexity.

In the build.gradle(Module: app) file, write down the below code lines

  • First line will enable us to draw a google map on our android screen.
  • Second line will give us access to the required classes which will help us to fetch the current location latitude and longitude of the user.
  • Third will allow us to use the dexter library which will simplify the process of runtime permissions in android.

Now in  AndroidManifest.xml file, add the below internet and location permissions.

We also have to add following code inside <application> tag.

Final and full code for AndroidManifest.xml file is as the following

Step 3. An interface

We need one interface in this example. So make a new class and give it a name

Source code for is as the below

Step 4. Making PointParser class

Prepare a new class and named it like class should contain the following code

  • Google will send us one JSON data, which will be parsed using this class.
  • When we want to draw the route, we need to parse one URL which consist the location (latitude and longitude) of both origin and destination places.

This URL has the below structure

Color of Path

I am drawing a red color path in this example.

If you want to change the color of this path, you need to update below lines

onPostExecute() method contains the above lines.

Step 5. FetchURL source code

Create a new JAVA class with name

Source Code block for is looking like the below

  • We will make the http call using the above class.
  • This class also holds the mode of transporat.
  • Right now the mode is “driving.
  • Possible values for mode is driving, bicycling, transit and walking.

To update the mode, update the below line

Step 6. Class of DataParser

Again make a new class and give it a name like

Following is the code structure for

Step 7. Main Modifications

Now final thing is to update activity_main.xml and files.

In the activity_main.xml file, add the below lines

  • Three widgets are there in this main layout file.
  • First is text view which will hold the value of the current latitude and longitude.
  • fragment will allow us to load google map inside itself.
  • When the user will click the button, we will draw the line between current location and destination on google map.

Now source code for is as the below

Let us dive deep in the above source code.

  • First of all, compiler will create some variables and objects of various classes like GoogleMap, MarkerOptions, Polyline, Location, LocationRequest etc.
  • Some of them are for google map and route and some will help to fetch current location.

Now read the onCreate() method.

  • Here, compiler will first call the requestMultiplePermissions() method.
  • This method will ask for runtime permissions.
  • Then compiler will initialize textview and mGoogleApiClient.
  • Compiler will call onMapReady() function when map has finished it’s loading.
  • getUrl() and onTaskDone() methods are the part of drawing the route process.
  • onConnected(), onConnectionSuspended(), onConnectionFailed(), onStart(), onStop() and startLocationUpdates() methods are the part of getting the current location.

Now read the onLocationChanged() method.

  • In this method, we will get current latitude and longitude.
  • Compiler will set the value of latitude and longitude in text view and then it will show one toast.
  • After that, it will check one if condition. Compiler will go inside this if condition only once.
  • Inside if condition, compiler will set the code for button’s click event.

Following line will draw the path.

Then compiler will execute the map loading process.

How to draw path when user is moving

Here, we are drawing the path on the user click only.

If you want to draw path with the movement of the user then simply write below line inside onLocationChanged() method instead of writing it inside button click.

Source code for this may be looking like

Above code will constantly draw the line as per the movement of the user.

May be you get some errors

There are possibility for two types of errors in this example.

First is something like “This API project is not authorized to use this API“.

See the following image

android google map draw path between current location and destination
Search for “mylog” in logcat as per above picture.

I was getting this error and it took me 6 hours to solve this. And the problem was that my Directions API was not enable !!

So do not waste your time when you are getting this error and quickly enable Directions API. (This is the prime intention of this blog to help you solve this type of simple errors that might eat your big amount of time)

Second possible error is that “You have exceeded your daily request” like following picture

This error is saying that your free quota for today is over. So you need to wait for 24 hours or you can create a new API to go further.

Download Code for android google map draw path between current location and destination

Download Code For Google_Map_CurrentLocation Route

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...

7 Responses

  1. Abdul says:

    Hey Thanks a lot. But please help me.
    I want to show someone continous location on map from Databse.I am saving continue location on Database. Now I want to show his position but location from database.

  2. zahid says:

    hi, This API project is not authorized to use this API this is come after enabling direction API, How can I solve plz help me

  3. pitipong watawut says:

    Hi Hardik, Is it normal to request to get the direction just once per day? because I got the error like you said “You have exceeded your daily request” when I tried to get the direction again.

    • Hardik Parsania says:

      It is depended on your requirement. For real time app like, uber or food delivery, you need to request for many times. You can purchase more requests per day from google if you exceed daily quota.

  4. sam patel says:

    Hii , Tutorial is amazing , but i have one question , How can i draw path with change location of marker and start path from starting point ? like Swiggy food app (Delivery food time)

  5. moulay says:

    hi i have a problem an message said direction not found

Leave a Reply

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