How to launch Google Maps Directions via an intent on Android?


Introduction

Many times in android applications the address to the specific store or a location is being displayed in the form of an icon. On clicking on that google map icon the application redirects us to the Google Maps application in which we will get to see the route from users location to the stores location. In this article we will take a look on How to launch Google Maps Directions via an intent on Android.

Implementation

We will be creating a simple application in which we will be creating a text view for displaying the heading of our application. After that we will be creating two text input fields which are used to get the source location and destination location from the user. Then we are displaying a button which will launch the google maps intent and draw a route between this source location and destination location. Now let's move towards android studio for creating a new project.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">
   <!-- on below line creating a text view for displaying the heading of our application -->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginStart="10dp"
       android:layout_marginTop="100dp"
       android:layout_marginEnd="10dp"
       android:gravity="center"
       android:padding="4dp"
       android:text="Google Maps Direction Intent in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />
   <!-- on below line creating edit text for source location -->
   <EditText
       android:id="@+id/idEdtSourceLocation"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_margin="10dp"
       android:hint="Enter Source Location" />
   <!-- on below line creating edit text for destination location -->
   <EditText
       android:id="@+id/idEdtDestinationLocation"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idEdtSourceLocation"
       android:layout_margin="10dp"
       android:hint="Enter Destination Location" />
   <!-- on below line creating a button to show direction on maps-->
   <Button
       android:id="@+id/idBtnShowDirection"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idEdtDestinationLocation"
       android:layout_margin="10dp"
       android:padding="4dp"
       android:text="Show Direction on Maps"
       android:textAllCaps="false" />
</RelativeLayout>

Explanation − In the above code we are creating a root layout as a Relative Layout. Inside this layout we are creating a text view which is used to display the heading of our application. After that we are creating two edit text fields which are use to get the source and destination location from the user. Then we are creating a button which is use to open the google maps intent and draw the route on google maps application.

Step 3 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.java_test_application;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variables for edit text on below line.
   private EditText sourceEdt, destinationEdt;
   // creating variable for button on below line.
   private Button directionBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables for edit text on below line.
       sourceEdt = findViewById(R.id.idEdtSourceLocation);
       destinationEdt = findViewById(R.id.idEdtDestinationLocation);
       // initializing variables for button on below line.
       directionBtn = findViewById(R.id.idBtnShowDirection);
       // adding click listener for button on below line.
       directionBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               if (sourceEdt.getText().toString().isEmpty() && destinationEdt.getText().toString().isEmpty()) {
                   // displaying toast message to enter source and destination location.
                   Toast.makeText(MainActivity.this, "Please enter source and destination location..", Toast.LENGTH_SHORT).show();
               } else {
                   // on below line calling open maps intent method to open maps intent and passing source and destination location to it.
                   openMapsIntent(sourceEdt.getText().toString(), destinationEdt.getText().toString());
               }
           }
       });
   }
   // on below line creating an open maps intent method.
   private void openMapsIntent(String source, String destination) {
       try {
           // create a uri on below line and passing a google maps url to open with source and destination location.
           Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + source + "/" + destination);
           // initializing a intent with action view and uri on below line.
           Intent i = new Intent(Intent.ACTION_VIEW, uri);
           // below line is to set maps package name for google maps.
           i.setPackage("com.google.android.apps.maps");
           // below line is to set flags to create a new task for google maps.
           i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           // start activity on below line.
           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.
           Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
           // initializing intent with action view.
           Intent i = new Intent(Intent.ACTION_VIEW, uri);
           // set flags on below line.
           i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           // on below line we are starting the activity.
           startActivity(i);
       }
   }
}

Explanation : In the above code firstly we are creating variables for our button and edit text. Now we will get to see the onCreate method. This is the default method of every android application. This method is called when the application view is created. Inside this method we are setting the content view i.e the layout file named activity_main.xml to set the UI from that file. Inside the onCreate method we are initializing the variables for button and edittext with the id which we have given in our activity_main.xml file. After that we are adding an on click listener for the button. Inside the on click listener we are firstly checking if the source and destination edit text is empty or not. If it is empty we are displaying a toast message to enter source and destination location. If the source and destination edittext is not empty we are calling open maps intent method in which we are passing source and destination location to open google maps intent.

Now in the open maps intent method we are creating a uri in which we are passing source and destination location. After that we are creating an intent in which we are setting google maps application package name to it. Then we are calling a start activity method to start google maps application. Along with that we are putting this in a try and catch block. In the catch block we are checking if the google maps application is not installed on the users device. In that case we are opening a google play application and redirecting users to install google maps application page to install google maps application.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above article we have taken a look on How to launch Google Maps Direction via an intent on an android application.

Updated on: 08-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements