Open In App

How to Draw a Track on Google Maps in Android?

Last Updated : 09 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--edit text for our source location-->
    <EditText
        android:id="@+id/idEdtSource"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Source"
        android:padding="8dp" />
  
    <!--edit text for our destination location-->
    <EditText
        android:id="@+id/idEdtDestination"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Destination"
        android:padding="8dp" />
  
    <!--button to draw route on maps-->
    <Button
        android:id="@+id/idBtnTrack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@color/purple_500"
        android:padding="8dp"
        android:text="Show Track"
        android:textAllCaps="false"
        android:textColor="@color/white" />
      
</LinearLayout>


Step 3: Working with the MainActivity.java file

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

Java




import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // creating variables for 
    // edit texts and button.
    EditText sourceEdt, destinationEdt;
    Button trackBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our edit text and buttons
        sourceEdt = findViewById(R.id.idEdtSource);
        destinationEdt = findViewById(R.id.idEdtDestination);
        trackBtn = findViewById(R.id.idBtnTrack);
  
        // adding on click listener to our button.
        trackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            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("https://www.google.co.in/maps/dir/" + source + "/" + destination);
              
            // initializing a intent with action view.
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
              
            // below line is to set maps package name
            i.setPackage("com.google.android.apps.maps");
              
            // below line is to set flags
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              
            // start activity
            startActivity(i);
        } 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
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             
            // to start activity
            startActivity(i);
        }
    }
}


Run your app and see the output of the app.

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads