android - 以编程方式在谷歌地图android中设置从位置到当前位置

标签 android google-maps google-maps-urls

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

这是我使用的代码。

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

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

最佳答案

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

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

origin: Defines the starting point from which to display directions. Defaults to most relevant starting location, such as user location, if available. If none, the resulting map may provide a blank form to allow a user to enter the origin. The value can be either a place name, address, or comma-separated latitude/longitude coordinates. A string should be URL-escaped.

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

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

希望对您有所帮助!

关于android - 以编程方式在谷歌地图android中设置从位置到当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59511400/

相关文章:

iOS GoogleMaps StreetView URL Scheme with Heading and Pitch

android - 无法访问 Foo 类型的封闭实例

java - 在Android中使用Bundle将ArrayList类对象传递给另一个 fragment

android - 无法从 Android 中的 SharedPreferences 访问字符串

iphone - 如何在 iPhone 中滚动 map 时跟踪 map 区域

Android:如何在 MapView 上执行附近搜索

java - 在python中压缩音频流数据并在java中解压缩

javascript - 谷歌地图,加载点上的纬度/经度

android - 通过 Intent 在android中的特定位置打开谷歌地图

javascript - 在 Google Maps API 中生成 Google Maps URL