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
- Define the URI. For turn-by-turn navigation, Google Maps uses the
google.navigation:scheme. (Thegeo:scheme is for displaying a map or searching for a location, not for directions.)
- Create the Intent with
Intent.ACTION_VIEWand restrict it to the Google Maps package:
- Check and start the activity. Verify that an app can handle the intent first, to avoid a crash:
Parameters for the navigation URI
The basic URI only needs a destination, but you can append:
- Travel mode:
&mode=dfor driving (the default),wfor walking,bfor bicycling, orlfor two-wheeler. - Avoidances:
&avoid=followed by one or more oft(tolls),h(highways) andf(ferries), for example&avoid=tf.
Example: driving to the Empire State Building
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
| Feature | Description | Example |
| URI Scheme | Navigation URI understood by Google Maps | google.navigation:q=latitude,longitude |
| Modes | Specifies the type of navigation | &mode=d for driving |
| Avoidances | Criteria to exclude certain routes | &avoid=t to avoid toll roads |
| Action | Android Intent action to view the directions | Intent.ACTION_VIEW |
Related reading
- LaunchScreen.xib not displaying my custom font
- Layout issues after updating to Xcode 8
- ld framework not found Pods
- Leading zeros for Int in Swift
- Leaking views when changing rootViewController inside transitionWithView
- Left-align image and center text on UIButton
- Left Align Cells in UICollectionView
- Lesser than or greater than in Swift switch statement
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.