Explain Codes LogoExplain Codes Logo

Launching Google Maps Directions via an Intent on Android

java
prompt-engineering
best-practices
android-development
Nikita BarsukovbyNikita Barsukov·Jan 5, 2025
TLDR

Quickly launch Google Maps Directions using the following intent snippet:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=destination_latitude,destination_longitude")); // 🗺️ Let's hit the road! startActivity(intent);

Simply, replace destination_latitude,destination_longitude with the precise coordinates. This fires up an intent that opens straight into Google Maps' navigation mode.

Or maybe you fancy address based directions?

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=an+address_city")); startActivity(intent);

Input the street address (replace spaces with "+") in place of "an+address_city" to utilize this intent.

Handling map navigation: best practices

Taking advantage of cross-platform Google Maps URLs

Google suggests using the Google Maps URLs API since May 2017. This cross-platform approach crafts intents like a pro:

Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/dir/?api=1&destination=address_or_latitude,longitude"); Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); // I choose you, Google Maps! if (mapIntent.resolveActivity(getPackageManager()) != null) { startActivity(mapIntent); // 🚀 Liftoff! }

Swap address_or_latitude,longitude with your desired destination. The package set ensures our intent lands in Google Maps.

Wooing the users

For improved user clarity, try including source and destination names:

String uri = String.format(Locale.ENGLISH, "https://www.google.com/maps/dir/?api=1&origin=%1$s&destination=%2$s", sourceName, destinationName); // "I'm at the office, boss!" Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); intent.setPackage("com.google.android.apps.maps"); // Only the best for you, boss! startActivity(intent);

This gives our users recognizable landmark names, over raw latitude-longitude coordinates.

Acting smart when Google Maps is MIA

If Google Maps is not installed, catch the ActivityNotFoundException and redirect to a web browser:

try { startActivity(mapIntent); // Fly, you fool! } catch (ActivityNotFoundException ex) { Uri webpageUri = Uri.parse("https://www.google.com/maps/dir/?api=1&destination=address_or_latitude,longitude"); // Backup arrived! Intent webIntent = new Intent(Intent.ACTION_VIEW, webpageUri); startActivity(webIntent); // Second try's the charm! }

Expanding the intent powers

Adding more features to the intents is as simple as adding more parameters:

Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/dir/?api=1&destination=address_or_latitude,longitude&mode=walking"); // The trees are lovely this season! 🚶‍♂️ Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); startActivity(mapIntent);

The mode parameter could be set to driving, walking, bicycling, or transit.

Squashing common bugs

Can't find an address or no route

If Google Maps fails to find an address or maps out no route, ensure:

  • Accurate address formatting. Use pluses (+) or URL encoding for spaces.
  • The Coordinates are correct and logged correctly.

Juggling multiple languages

Proper locale matters while formatting URIs:

String uri = String.format(Locale.ENGLISH, "google.navigation:q=%f,%f", latitude, longitude); // Precision is key!

Ensures decimal points in coordinates, are always ".", not ",".

A "how-to" on custom labelling map points

Uri geoLocation = Uri.parse(String.format(Locale.ENGLISH, "geo:%f,%f?q=%f,%f(Awesome+Place)", latitude, longitude, latitude, longitude)); // 📍 Add your mark! Intent intent = new Intent(Intent.ACTION_VIEW, geoLocation); startActivity(intent);

Use cases like labelled destinations can benefit from this feature.