我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?


当前回答

谷歌方向视图,源位置作为当前位置,目标位置作为给定的字符串

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&daddr="+destinationCityName));
intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

在上面的destinationCityName是一个字符串变量,可以根据需要进行修改。

其他回答

你可以使用这样的代码:

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

若要从当前位置开始导航,请删除saddr参数和值。

您可以使用实际的街道地址,而不是经纬度。然而,这将给用户一个对话框,选择通过浏览器打开它或谷歌地图。

这将在导航模式下直接启动谷歌地图:

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

更新

2017年5月,谷歌推出了通用、跨平台谷歌地图url的新API:

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

你也可以在新的API中使用intent。

试试这个

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

使用最新的跨平台谷歌地图url: 即使谷歌地图应用程序是缺失的,它将在浏览器中打开

例如https://www.google.com/maps/dir/?api=1&origin=81.23444 67.0000目的地= 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);

你可以尝试通过Intent打开内置应用Android Maps。setClassName方法。

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

这有点跑题了,因为你问了“方向”,但你也可以使用Android文档中描述的Geo URI方案:

http://developer.android.com/guide/appendix/g-app-intents.html

使用“geo:latitude,longitude”的问题是谷歌Maps仅以您的点为中心,没有任何大头针或标签。

这很让人困惑,尤其是当你需要指明一个确切的地方或问路的时候。

如果使用查询参数“geo:lat,lon?”Q =name”为了标记您的地理点,它使用查询来搜索并取消lat/lon参数。

我找到了一种方法,以纬度/lon居中的地图和显示一个自定义标签的大头针,非常好的显示和有用的时候,询问方向或任何其他行动:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
startActivity(intent);

注意(by @TheNail):在地图v.7中不能工作(撰写本文时的最新版本)。将忽略坐标并搜索圆括号之间具有给定名称的对象。请参见谷歌将7.0.0映射为location的Intent