Launching Google Maps Directions via an Intent on Android
Quickly launch Google Maps Directions using the following intent snippet:
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?
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:
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:
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:
Expanding the intent powers
Adding more features to the intents is as simple as adding more parameters:
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:
Ensures decimal points in coordinates, are always ".", not ",".
A "how-to" on custom labelling map points
Use cases like labelled destinations can benefit from this feature.
Was this article helpful?