我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?
当前回答
这有点跑题了,因为你问了“方向”,但你也可以使用Android文档中描述的Geo URI方案:
http://developer.android.com/guide/appendix/g-app-intents.html
使用“geo:latitude,longitude”的问题是谷歌Maps仅以您的点为中心,没有任何大头针或标签。
这很让人困惑,尤其是当你需要指明一个确切的地方或问路的时候。
如果使用查询参数“geo:lat,lon?”Q =name”为了标记您的地理点,它使用查询来搜索并取消lat/lon参数。
我找到了一种方法,以纬度/lon居中的地图和显示一个自定义标签的大头针,非常好的显示和有用的时候,询问方向或任何其他行动:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
startActivity(intent);
注意(by @TheNail):在地图v.7中不能工作(撰写本文时的最新版本)。将忽略坐标并搜索圆括号之间具有给定名称的对象。请参见谷歌将7.0.0映射为location的Intent
其他回答
你可以使用这样的代码:
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参数和值。
您可以使用实际的街道地址,而不是经纬度。然而,这将给用户一个对话框,选择通过浏览器打开它或谷歌地图。
这将在导航模式下直接启动谷歌地图:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=an+address+city"));
更新
2017年5月,谷歌推出了通用、跨平台谷歌地图url的新API:
https://developers.google.com/maps/documentation/urls/guide
你也可以在新的API中使用intent。
这有点跑题了,因为你问了“方向”,但你也可以使用Android文档中描述的Geo URI方案:
http://developer.android.com/guide/appendix/g-app-intents.html
使用“geo:latitude,longitude”的问题是谷歌Maps仅以您的点为中心,没有任何大头针或标签。
这很让人困惑,尤其是当你需要指明一个确切的地方或问路的时候。
如果使用查询参数“geo:lat,lon?”Q =name”为了标记您的地理点,它使用查询来搜索并取消lat/lon参数。
我找到了一种方法,以纬度/lon居中的地图和显示一个自定义标签的大头针,非常好的显示和有用的时候,询问方向或任何其他行动:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
startActivity(intent);
注意(by @TheNail):在地图v.7中不能工作(撰写本文时的最新版本)。将忽略坐标并搜索圆括号之间具有给定名称的对象。请参见谷歌将7.0.0映射为location的Intent
你可以通过这种方式在Android上启动谷歌地图方向
btn_search_route.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String source = et_source.getText().toString();
String destination = et_destination.getText().toString();
if (TextUtils.isEmpty(source)) {
et_source.setError("Enter Soruce point");
} else if (TextUtils.isEmpty(destination)) {
et_destination.setError("Enter Destination Point");
} else {
String sendstring="http://maps.google.com/maps?saddr=" +
source +
"&daddr=" +
destination;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(sendstring));
startActivity(intent);
}
}
});
尽管当前的答案是伟大的,没有人做很我在寻找什么,我想打开地图应用,添加一个名称为每个源位置和目的地,使用地理URI方案不会为我工作,地图网页链接没有标签我想出了这个解决方案,这本质上是一个合并的其他解决方案和评论,希望它可以帮助其他人查看这个问题。
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);
为了完整起见,如果用户没有安装地图应用,那么捕捉ActivityNotFoundException是一个好主意,然后我们可以在没有地图应用限制的情况下再次启动活动,我们可以很确定我们永远不会在最后到达Toast,因为互联网浏览器也是启动这个url方案的有效应用。
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 12f, 2f, "Where the party is at");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
intent.setPackage("com.google.android.apps.maps");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException ex)
{
try
{
Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(unrestrictedIntent);
}
catch(ActivityNotFoundException innerEx)
{
Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
}
}
注: 在我的例子中使用的任何纬度或经度都不能代表我的位置,任何与真实位置的相似性都是纯粹的巧合,也就是说我不是来自非洲:P
编辑:
对于方向,google.navigation现在支持导航意图
Uri navigationIntentUri = Uri.parse("google.navigation:q=" + 12f +"," + 2f);//creating intent with latlng
Intent mapIntent = new Intent(Intent.ACTION_VIEW, navigationIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
试试这个
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+src_lat+","+src_ltg+"&daddr="+des_lat+","+des_ltg));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
推荐文章
- Android是否保留。apk文件?如果有,在哪里?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?
- 什么时候我们应该使用观察者和可观察对象?