Launching Google Maps Directions via an intent on

2018-12-31 06:54发布

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?

14条回答
公子世无双
2楼-- · 2018-12-31 07:30

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:

  • d for driving
  • w for walking
  • b for bicycling

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.

查看更多
梦该遗忘
3楼-- · 2018-12-31 07:31

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"));

UPDATE

In May 2017 Google launched the new API for universal, cross-platform Google Maps URLs:

https://developers.google.com/maps/documentation/urls/guide

You can use Intents with the new API as well.

查看更多
零度萤火
4楼-- · 2018-12-31 07:34

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);
查看更多
听够珍惜
5楼-- · 2018-12-31 07:37

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);
}
查看更多
姐姐魅力值爆表
6楼-- · 2018-12-31 07:38

For multiple way points, following can be used as well.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("https://www.google.com/maps/dir/48.8276261,2.3350114/48.8476794,2.340595/48.8550395,2.300022/48.8417122,2.3028844"));
startActivity(intent);

First set of coordinates are your starting location. All of the next are way points, plotted route goes through.

Just keep adding way points by concating "/latitude,longitude" at the end. There is apparently a limit of 23 way points according to google docs. Not sure if that applies to Android too.

查看更多
余欢
7楼-- · 2018-12-31 07:39

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

查看更多
登录 后发表回答