Android Development
Google Maps
Coding
Mobile Applications
Software Development

Launch Google Maps directions via an intent on Android (google.navigation)

Codemia Knowledge Hub ·

Build a google.navigation:q=<lat>,<lng> URI, wrap it in an Intent with Intent.ACTION_VIEW, set the package to com.google.android.apps.maps, and start the activity after checking that something can handle it. Google Maps then opens with directions to that point.

What an Intent does here

An Android Intent is a messaging object that requests an action from another app component. Here it asks the Google Maps app to start an activity that shows the route.

Step by step

  1. Define the URI. For turn-by-turn navigation, Google Maps uses the google.navigation: scheme. (The geo: scheme is for displaying a map or searching for a location, not for directions.)
java
    String uri = "google.navigation:q=" + latitude + "," + longitude;
  1. Create the Intent with Intent.ACTION_VIEW and restrict it to the Google Maps package:
java
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    intent.setPackage("com.google.android.apps.maps");
  1. Check and start the activity. Verify that an app can handle the intent first, to avoid a crash:
java
1    if (intent.resolveActivity(getPackageManager()) != null) {
2        startActivity(intent);
3    } else {
4        // Handle the error case where no application can handle the intent.
5    }

Parameters for the navigation URI

The basic URI only needs a destination, but you can append:

  • Travel mode: &mode=d for driving (the default), w for walking, b for bicycling, or l for two-wheeler.
  • Avoidances: &avoid= followed by one or more of t (tolls), h (highways) and f (ferries), for example &avoid=tf.

Example: driving to the Empire State Building

java
1String uri = "google.navigation:q=40.748817,-73.985428&mode=d";
2Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
3mapIntent.setPackage("com.google.android.apps.maps");
4
5if (mapIntent.resolveActivity(getPackageManager()) != null) {
6    startActivity(mapIntent);
7} else {
8    // Code to inform the user that no apps can handle this intent.
9}

Permissions and a missing Maps app

  • Permissions: launching Google Maps for directions with an intent needs no extra permissions.
  • Google Maps must be installed: otherwise intent resolution fails, and your app should handle that gracefully.

Quick reference

FeatureDescriptionExample
URI SchemeNavigation URI understood by Google Mapsgoogle.navigation:q=latitude,longitude
ModesSpecifies the type of navigation&mode=d for driving
AvoidancesCriteria to exclude certain routes&avoid=t to avoid toll roads
ActionAndroid Intent action to view the directionsIntent.ACTION_VIEW

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms