我正在创建一个应用程序,它需要打开谷歌地图才能以步行模式导航到某个地方并将源/起始位置设置为当前位置。
这是我使用的代码。
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/