通过 Android 上的 Intent 启动 Google 地图路线 [英] Launching Google Maps Directions via an intent on Android
问题描述
我的应用程序需要显示从 A 到 B 的 Google 地图路线,但我不想将 Google 地图放入我的应用程序 - 相反,我想使用 Intent 启动它.这可能吗?如果是,如何?
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. Is this possible? If yes, how?
推荐答案
你可以这样使用:
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
参数和值.
To start the navigation from the current location, remove the saddr
parameter and value.
您可以使用实际街道地址而不是纬度和经度.但是,这将为用户提供一个对话框,供用户选择是通过浏览器还是 Google 地图打开它.
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.
这将直接在导航模式下启动 Google 地图:
This will fire up Google Maps in navigation mode directly:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=an+address+city"));
更新
2017 年 5 月,Google 推出了用于通用跨平台 Google 地图网址的新 API:
In May 2017 Google launched the new API for universal, cross-platform Google Maps URLs:
https://developers.google.com/maps/documentation/urls/guide
您也可以在新 API 中使用 Intent.
You can use Intents with the new API as well.
这篇关于通过 Android 上的 Intent 启动 Google 地图路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!