Skip to content
Related Articles

Related Articles

How to Draw a Track on Google Maps in Android?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 09 Feb, 2021

In many android apps, we have seen that there is a route marker from a source location to the destination location. In this article, we will take a look at How we can draw a track on Google Maps in Android. 

What we are going to build in this article? 

We will be building a simple application in which we will be displaying two EditText fields inside that fields we have to enter a source location and a destination location and we will display a button to display a route. After clicking on that button we will display a route on Google Maps for that location along with time. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

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. 


<?xml version="1.0" encoding="utf-8"?>
    <!--edit text for our source location-->
        android:padding="8dp" />
    <!--edit text for our destination location-->
        android:padding="8dp" />
    <!--button to draw route on maps-->
        android:text="Show Track"
        android:textColor="@color/white" />

Step 3: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    // creating variables for 
    // edit texts and button.
    EditText sourceEdt, destinationEdt;
    Button trackBtn;
    protected void onCreate(Bundle savedInstanceState) {
        // initializing our edit text and buttons
        sourceEdt = findViewById(;
        destinationEdt = findViewById(;
        trackBtn = findViewById(;
        // adding on click listener to our button.
        trackBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // calling a method to draw a track on google maps.
                drawTrack(sourceEdt.getText().toString(), destinationEdt.getText().toString());
    private void drawTrack(String source, String destination) {
        try {
            // create a uri
            Uri uri = Uri.parse("" + source + "/" + destination);
            // initializing a intent with action view.
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            // below line is to set maps package name
            // below line is to set flags
            // start activity
        } catch (ActivityNotFoundException e) {
            // when the google maps is not installed on users device
            // we will redirect our user to google play to download google maps.
            // initializing intent with action view.
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
            // set flags
            // to start activity

Run your app and see the output of the app.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!