Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:打开带有两点方向的地图意图_Android_Google Maps - Fatal编程技术网

Android:打开带有两点方向的地图意图

Android:打开带有两点方向的地图意图,android,google-maps,Android,Google Maps,如何打开地图意图与方向 我知道 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Locale.US, "geo:%.8f,%.8f", latitude, longitude))); startActivity(Intent.createChooser(intent, "Select an application")); 这段代码在一点上运行良好。但我想显示所有地图应用程序的对话框,并用任何应用程序(

如何打开地图意图与方向

我知道

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Locale.US, "geo:%.8f,%.8f", latitude, longitude)));
startActivity(Intent.createChooser(intent, "Select an application"));
这段代码在一点上运行良好。但我想显示所有地图应用程序的对话框,并用任何应用程序(谷歌地图、城市地图、网络浏览器等)打开从A点到B点的方向。

您尝试过这个吗

String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+latitude1+","+longitude1+"&daddr="+latitude2+","+longitude2;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(Intent.createChooser(intent, "Select an application"));

不幸的是,您不能使用通用URI来表示两个地理点

可以指定单个点并将其传递给选择器,如:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lng);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(Intent.createChooser(intent, "Select your maps app"));
然而,不幸的是,要获得方向,您需要使用:

单个URL:

在我看来,这是最好的选择。您应该使用单个地图供应商,并使用其URL作为URI。这样,您得到的方向是一致的,与多个设备兼容,最重要的是易于使用/获取

普通的旧URL:

您可以为不同的地图供应商使用不同的URL

例如,Citymapper:

然而,其他人并不那么友好,例如Yandex:

也许您可以自己解码该url:)

集成地图API

将Google Maps API等集成到您的应用程序中,并从中显示方向。
您需要以JSON格式从directions API获取lat lng点格式的方向。然后,您将使用这些点绘制多段线。

如果您以以下格式创建URL

http://maps.google.com/maps?saddr=[latitude1], [longitude1]&daddr=[latitude2], [longitude2]
其中,[latitude1]和[longitude1]是起点的纬度和经度,同样地,[latitude2]和[longitude2]是终点,并将其设置为字符串

Intent navigationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(navigationUrlOfMap));
intent.setClassName("com.Google.Android.apps.maps", "com.Google.Android.maps.MapsActivity");
 startActivity(intent); 
或者你也可以用这个

startActivity(Intent.createChooser(intent, "Select an application"));
如果要设置选择器应用程序,请尝试以下操作

Uri uri = Uri.parse("geo:" + latitude1 + "," + longitude1 + "?q=" + Uri.encode(latitude2 + "," + longitude2 + "(" +Label+ ")"));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);

谢谢,我会用其他应用程序(1-2天)检查它,因为“Citymapper”对我不起作用(崩溃),“Ynadex Maps”(不支持此api?)也不起作用。第一个也是最好的答案+50.这应该围绕着try/catch,因为如果没有找到处理请求的活动,这将是一个例外!有没有办法通过Intent在Google地图上显示多段线?谢谢,我知道:)我需要一个通用的方法,请参阅Foursquare@monyag我已经检查过了,正如我所说的,它没有使用通用URI,因为没有。它只是提供了一个谷歌地图的网址。非常感谢!这就是我一直在寻找的答案。最近,“…”直接打开GMAP,即使我使用Intent.createChooser
startActivity(Intent.createChooser(intent, "Select an application"));
Uri uri = Uri.parse("geo:" + latitude1 + "," + longitude1 + "?q=" + Uri.encode(latitude2 + "," + longitude2 + "(" +Label+ ")"));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);