通过Android上的意图启动Google地图路线

本文翻译自:Launching Google Maps Directions via an intent on Android

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. 我的应用需要显示从A到B的Google地图路线,但我不想将Google地图放入我的应用程序 - 相反,我想使用Intent启动它。 Is this possible? 这可能吗? If yes, how? 如果有,怎么样?


#1楼

参考:https://stackoom.com/question/BAe3/通过Android上的意图启动Google地图路线


#2楼

if you know point A, point B (and whatever features or tracks in between) you can use a KML file along with your intent. 如果你知道A点,B点(以及其间的任何特征或轨迹),你可以使用KML文件和你的意图。

String kmlWebAddress = "http://www.afischer-online.de/sos/AFTrack/tracks/e1/01.24.Soltau2Wietzendorf.kml";
String uri = String.format(Locale.ENGLISH, "geo:0,0?q=%s",kmlWebAddress);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);

for more info, see this SO answer 有关更多信息,请参阅此SO答案

NOTE: this example uses a sample file that (as of mar13) is still online. 注意:此示例使用的示例文件(截至3月13日)仍处于联机状态。 if it has gone offline, find a kml file online and change your url 如果已脱机,请在线查找kml文件并更改您的网址


#3楼

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. 虽然目前的答案很棒,但没有一个能达到我想要的效果,我只想打开地图应用程序,为每个源位置和目的地添加一个名称,使用geo URI方案对我来说不起作用在所有和地图网站链接没有标签所以我想出了这个解决方案,这基本上是这里的其他解决方案和评论的合并,希望它有助于其他人查看这个问题。

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. 为了完整性,如果用户没有安装地图应用程序,那么捕获ActivityNotFoundException将是一个好主意,然后我们可以在没有地图应用程序限制的情况下再次启动活动,我们可以肯定我们永远不会得到因为互联网浏览器也是启动此网址方案的有效应用程序,所以最后到Toast。

        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();
            }
        }

PS 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 PS我的例子中使用的任何纬度或经度都不能代表我的位置,任何与真实位置的相似之处纯属巧合,我也不是来自非洲:P

EDIT: 编辑:

For directions, a navigation intent is now supported with google.navigation 对于路线,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);

#4楼

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. 要从当前位置开始导航,请删除saddr参数和值。

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: 这将直接在导航模式下启动Google地图:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
    Uri.parse("google.navigation:q=an+address+city"));

UPDATE UPDATE

In May 2017 Google launched the new API for universal, cross-platform Google Maps URLs: 2017年5月,Google推出了适用于通用跨平台Google地图网址的新API:

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

You can use Intents with the new API as well. 您也可以将Intent与新API一起使用。


#5楼

try this 试试这个

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+src_lat+","+src_ltg+"&daddr="+des_lat+","+des_ltg));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

#6楼

Well you can try to open the built-in application Android Maps by using the Intent.setClassName method. 您可以尝试使用Intent.setClassName方法打开内置应用程序Android Maps。

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:37.827500,-122.481670"));
i.setClassName("com.google.android.apps.maps",
    "com.google.android.maps.MapsActivity");
startActivity(i);

你可能感兴趣的:(java,android,google-maps,android-intent)