String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?saddr=%f,%f(%s)&daddr=%f,%f (%s)", sourceLatitude, sourceLongitude, "Home Sweet Home", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
要使用当前位置作为起点(遗憾的是我还没有找到标记当前位置的方法),请使用以下内容
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", destinationLatitude, destinationLongitude, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
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);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
startActivity(intent);
NOTE (by @TheNail): Not working in Maps v.7 (latest version at the time of writing). Will ignore the coordinates and search for an object with the given name between the parentheses. See also Intent for Google Maps 7.0.0 with location
2
Google DirectionsView 将源位置作为当前位置和目标位置,以字符串形式给出
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 是一个字符串变量,根据需要修改它 .
1
如果您有兴趣从当前方向显示纬度和经度,您可以使用:
始终从用户当前位置给出方向 .
以下查询将帮助您执行该操作 . 您可以在此处传递目的地纬度和经度:
google.navigation:q=latitude,longitude
使用以上作为:
Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
String addressString = "1600 Amphitheatre Parkway, CA";
/*
Build the uri
*/
Uri.Builder builder = new Uri.Builder();
builder.scheme("geo")
.path("0,0")
.query(addressString);
Uri addressUri = builder.build();
/*
Intent to open the map
*/
Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
/*
verify if the devise can launch the map intent
*/
if (intent.resolveActivity(getPackageManager()) != null) {
/*
launch the intent
*/
startActivity(intent);
}
14 回答
虽然目前的答案很棒,但没有一个能达到我想要的效果,我只想打开 Map 应用程序,为每个源位置和目的地添加一个名称,使用geo URI方案对我来说不起作用在所有和 Map 网站链接没有标签所以我想出了这个解决方案,这基本上是在这里做出的其他解决方案和评论的合并,希望它有助于其他人查看这个问题 .
要使用当前位置作为起点(遗憾的是我还没有找到标记当前位置的方法),请使用以下内容
为了完整性,如果用户没有安装 Map 应用程序,那么捕获ActivityNotFoundException将是一个好主意,然后我们可以在没有 Map 应用程序限制的情况下再次启动活动,我们可以肯定我们永远不会得到因为互联网浏览器也是启动这个网址方案的有效应用程序,所以最后到Toast .
附:在我的例子中使用的任何纬度或经度都不能代表我的位置,任何与真实位置的相似之处纯属巧合,也就是说我不是来自非洲:P
EDIT:
对于路线,google.navigation现在支持导航意图
试试这个
使用最新的 cross-platform Google Maps URLs :即使谷歌 Map 应用程序丢失,它也会在浏览器中打开
示例https://www.google.com/maps/dir/?api=1&origin=81.23444,67.0000&destination=80.252059,13.060604
那么您可以尝试使用
Intent.setClassName
方法打开内置应用程序Android Maps .这有点偏离主题,因为您要求“方向”,但您也可以使用Android文档中描述的Geo URI方案:
http://developer.android.com/guide/appendix/g-app-intents.html
使用“geo:latitude,longitude”的问题在于Google Map 仅在您的位置居中,没有任何引脚或标签 .
这非常令人困惑,特别是如果你需要指向一个精确的地方或/并询问方向 .
如果使用查询参数“geo:lat,lon?q = name”来标记地理点,它将使用搜索查询并关闭lat / lon参数 .
我找到了一种用lat / lon中心 Map 的方法,并显示一个带有自定义标签的图钉,非常适合展示,并在询问路线或任何其他动作时非常有用:
NOTE (by @TheNail): Not working in Maps v.7 (latest version at the time of writing). Will ignore the coordinates and search for an object with the given name between the parentheses. See also Intent for Google Maps 7.0.0 with location
Google
DirectionsView
将源位置作为当前位置和目标位置,以字符串形式给出在上面
destinationCityName
是一个字符串变量,根据需要修改它 .如果您有兴趣从当前方向显示纬度和经度,您可以使用:
以下查询将帮助您执行该操作 . 您可以在此处传递目的地纬度和经度:
使用以上作为:
或者,如果您想通过位置显示,请使用:
更多信息:Google Maps Intents for Android
对于多个航路点,也可以使用以下内容 .
第一组坐标是您的起始位置 . 所有接下来都是路点,绘制的路线经过 .
只需通过在结尾处结束"/latitude,longitude"来保持添加方式点 . 根据google docs,显然有23个方向的限制 . 不确定这是否也适用于Android .
如果你知道A点,B点(以及其间的任何特征或轨迹),你可以使用KML文件和你的意图 .
有关更多信息,请参阅this SO answer
注意:此示例使用的示例文件(截至3月13日)仍处于联机状态 . 如果它已脱机,请在线查找kml文件并更改您的网址
首先你需要现在可以使用隐式意图,android文档为我们提供了一个非常详细的common intents用于实现你需要用两个参数创建一个新意图的 Map 意图
行动
Uri
对于行动,我们可以使用
Intent.ACTION_VIEW
,对于Uri,我们应该构建它,下面我附加了一个示例代码来创建,构建,启动活动 .Open Google Maps using Intent with different Modes:
我们可以使用意图打开Google Map 应用:
这里,“mode = b”适用于自行车 .
我们可以使用以下方式设置驾驶,步行和骑车模式:
d用于驾驶
w走路
b用于骑自行车
您可以使用Google Map here找到有关意图的更多信息 .
注意:如果没有自行车/汽车/步行路线,那么它会显示“找不到那里的路”
你可以查看我的原始答案here .
你可以使用这样的东西:
要从当前位置开始导航,请删除
saddr
参数和值 .您可以使用实际的街道地址而不是纬度和经度 . 但是,这将为用户提供一个对话框,可以选择通过浏览器还是Google打开它 Map .
这将直接在导航模式下启动Google Map :
UPDATE
2017年5月,Google推出了针对通用跨平台Google Map 网址的新API:
https://developers.google.com/maps/documentation/urls/guide
您也可以将Intent与新API一起使用 .
这对我有用: