My app needs to show Google Maps directions from A to B, but I don't want to put the Google Maps into my application - instead, I want to launch it using an Intent. Is this possible? If yes, how?
You could use something like this:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345")); startActivity(intent);
To start the navigation from the current location, remove the
saddr parameter and value.
You can use an actual street address instead of latitude and longitude. However this will give the user a dialog to choose between opening it via browser or Google Maps.
This will fire up Google Maps in navigation mode directly:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("google.navigation:q=an+address+city"));
In May 2017 Google launched the new API for universal, cross-platform Google Maps URLs:
You can use Intents with the new API as well.
This is a little off-topic because you asked for "directions", but you can also use the Geo URI scheme described in the Android Documentation:
The problem using "geo:latitude,longitude" is that Google Maps only centers at your point, without any pin or label.
That's quite confusing, especially if you need to point to a precise place or/and ask for directions.
If you use the query parameter "geo:lat,lon?q=name" in order to label your geopoint, it uses the query for search and dismiss the lat/lon parameters.
I found a way to center the map with lat/lon and display a pin with a custom label, very nice to display and useful when asking for directions or any other action:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")")); startActivity(intent);
NOTE (by @TheNail): Not working in Maps v.7 (latest version at the time of writing). Will ignore the coordinates and search for an object with the given name between the parentheses. See also Intent for Google Maps 7.0.0 with location