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
Using the latest cross-platform Google Maps URLs: Even if google maps app is missing it will open in browser
Example https://www.google.com/maps/dir/?api=1&origin=81.23444,67.0000&destination=80.252059,13.060604
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www.google.com")
.appendPath("maps")
.appendPath("dir")
.appendPath("")
.appendQueryParameter("api", "1")
.appendQueryParameter("destination", 80.00023 + "," + 13.0783);
String url = builder.build().toString();
Log.d("Directions", url);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Open Google Maps using Intent with different Modes:
We can open Google Maps app using intent:
val gmmIntentUri = Uri.parse("google.navigation:q="+destintationLatitude+","+destintationLongitude + "&mode=b")
val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
mapIntent.setPackage("com.google.android.apps.maps")
startActivity(mapIntent)
Here, "mode=b" is for bicycle.
We can set driving, walking, and bicycling mode by using:
You can find more about intent with google maps here.
Note: If there is no route for the bicycle/car/walk then it will show you "Can't find the way there"
You can check my original answer here.
First you need to now that you can use the implicit intent, android documentation provide us with a very detailed common intents for implementing the map intent you need to create a new intent with two parameters
For action we can use Intent.ACTION_VIEW
and for Uri we should Build it ,below i attached a sample code to create,build,start the activity.
String addressString = "1600 Amphitheatre Parkway, CA";
/*
Build the uri
*/
Uri.Builder builder = new Uri.Builder();
builder.scheme("geo")
.path("0,0")
.query(addressString);
Uri addressUri = builder.build();
/*
Intent to open the map
*/
Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
/*
verify if the devise can launch the map intent
*/
if (intent.resolveActivity(getPackageManager()) != null) {
/*
launch the intent
*/
startActivity(intent);
}