1

我正在创建一个应用程序,它需要打开谷歌地图以导航到步行模式的地方并将源/从位置设置为当前位置。

这是我使用的代码。

String url = "https://maps.google.co.in/maps?q=" + destination + "&mode=walking";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

如何将“从位置”设置为“当前位置”?谢谢。

4

1 回答 1

1

我建议使用Google Maps URLs而不是您在示例中提供的链接。Google 地图 URL 是在移动设备和网络浏览器上启动 Google 地图应用的官方推荐方式。

请注意,如果您origin在 Google 地图 URL 中省略该参数,API 将使用您的当前位置(如果可用)

origin : 定义显示方向的起点。默认为最相关的起始位置,例如用户位置(如果可用)。如果没有,则生成的地图可能会提供一个空白表格以允许用户输入原点。该值可以是地名、地址或以逗号分隔的纬度/经度坐标。字符串应该是 URL 转义的。

因此,您应该将代码重写为

String url = "https://www.google.com/maps/dir/?api=1&destination=" + destination + "&travelmode=walking&dir_action=navigate";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

我希望这有帮助!

于 2019-12-28T17:49:19.673 回答