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
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:
http://developer.android.com/guide/appendix/g-app-intents.html
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
This is what worked for me:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://maps.google.co.in/maps?q=" + yourAddress));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
A nice kotlin solution, using the latest cross-platform answer mentioned by lakshman sai...
No unnecessary Uri.toString and the Uri.parse though, this answer clean and minimal:
val intentUri = Uri.Builder().apply {
scheme("https")
authority("www.google.com")
appendPath("maps")
appendPath("dir")
appendPath("")
appendQueryParameter("api", "1")
appendQueryParameter("destination", "${yourLocation.latitude},${yourLocation.longitude}")
}.build()
startActivity(Intent(Intent.ACTION_VIEW).apply {
data = intentUri
})
Although the current answers are great, none of them did quite what I was looking for, I wanted to open the maps app only, add a name for each of the source location and destination, using the geo URI scheme wouldn't work for me at all and the maps web link didn't have labels so I came up with this solution, which is essentially an amalgamation of the other solutions and comments made here, hopefully it's helpful to others viewing this question.
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
To use your current location as the starting point (unfortunately I haven't found a way to label the current location) then use the following
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
For completeness, if the user doesn't have the maps app installed then it's going to be a good idea to catch the ActivityNotFoundException, then we can start the activity again without the maps app restriction, we can be pretty sure that we will never get to the Toast at the end since an internet browser is a valid application to launch this url scheme too.
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 12f, 2f, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException ex)
{
try
{
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(unrestrictedIntent);
}
catch(ActivityNotFoundException innerEx)
{
Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
}
}
P.S. Any latitudes or longitudes used in my example are not representative of my location, any likeness to a true location is pure coincidence, aka I'm not from Africa :P
EDIT:
For directions, a navigation intent is now supported with google.navigation
Uri navigationIntentUri = Uri.parse("google.navigation:q=" + 12f +"," + 2f);//creating intent with latlng
Intent mapIntent = new Intent(Intent.ACTION_VIEW, navigationIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
If you interested in showing the Latitude and Longitude from the current direction , you can use this :
Directions are always given from the users current location.
The following query will help you perform that . You can pass the destination latitude and longitude here:
google.navigation:q=latitude,longitude
Use above as:
Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Or if you want to show via location , use:
google.navigation:q=a+street+address
More Info here: Google Maps Intents for Android
You can Launch Google Maps Directions via an intent on Android through this way
btn_search_route.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String source = et_source.getText().toString();
String destination = et_destination.getText().toString();
if (TextUtils.isEmpty(source)) {
et_source.setError("Enter Soruce point");
} else if (TextUtils.isEmpty(destination)) {
et_destination.setError("Enter Destination Point");
} else {
String sendstring="http://maps.google.com/maps?saddr=" +
source +
"&daddr=" +
destination;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(sendstring));
startActivity(intent);
}
}
});