3

我正在开发我发送waypointsGmaps应用程序的功能,Intent以便用户可以通过waypoints我发送的自定义导航到目的地

当我在我的嵌入式中绘制这条路线时Google Maps,我可以看到Circuit route,但是当我在Gmapsapp 中看到相同的路线时,它就Circuit坏了。

我的代码:

String srcAdd = "saddr="+latLngArrayList.get(0).latitude+","+latLngArrayList.get(0).longitude;
        String desAdd = "&daddr="+latLngArrayList.get(latLngArrayList.size() - 1).latitude+","+latLngArrayList.get(latLngArrayList.size() - 1).longitude;
        String wayPoints = "";

        for (int j = 1; j < latLngArrayList.size() - 1; ++j) {

            wayPoints =wayPoints+"+to:"+latLngArrayList.get(j).latitude+","+latLngArrayList.get(j).longitude;
        }

        String link="https://maps.google.com/maps?"+srcAdd+desAdd+wayPoints;
        final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(link));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
        startActivity(intent);

电路路线 在此处输入图像描述

没有线路 在此处输入图像描述

4

1 回答 1

9

我建议看一下2017 年 5 月推出的Google Maps URLs API。该 API 提供了通用的跨平台链接,您可以在应用程序中使用这些链接来启动 Google Maps 的意图。支持的模式之一是方向模式。你可以在这里阅读。

当您使用 Directions API 并发布路点的示例坐标时,我能够在网络服务和 Google 地图 URL 中测试结果。

Web 服务结果在方向计算器工具中进行了测试:

https://directionsdebug.firebaseapp.com/?origin=19.07598304748535%2C72.87765502929688&destination=19.07598304748535%2C72.87765502929688&waypoints=18.7284%2C73.4815%7C18.6876%2C73.4827%7C18.5839587%2C73.5125092%7C18.5369444 %2C73.4861111%7C18.480567%2C73.491658

我们通过 Directions API 获得的路线如下:

在此处输入图像描述

这些航点的 Google 地图 URL 链接如下:

https://www.google.com/maps/dir/?api=1&origin=19.07598304748535,72.87765502929688&destination=19.07598304748535,72.87765502929688&waypoints=18.7284,73.4815%7C18.6876,73.4827%7C18.5839587,73.5125092%7C18.5369444,73.4861111 %7C18.480567,73.491658&travelmode=驾驶

您使用 Google 地图 URL 获得的路线显示在此屏幕截图中。

在此处输入图像描述

如您所见,两条路线是相同的,因此 Directions API 和 Google Maps URL 可以正常工作。我认为您应该更改代码以使用 Google 地图网址:

String srcAdd = "&origin=" + latLngArrayList.get(0).latitude + "," + latLngArrayList.get(0).longitude;  
String desAdd = "&destination=" + latLngArrayList.get(latLngArrayList.size() - 1).latitude + "," + latLngArrayList.get(latLngArrayList.size() - 1).longitude;
String wayPoints = "";  

for (int j = 1; j < latLngArrayList.size() - 1; j++) {
    wayPoints = wayPoints + (wayPoints.equals("") ? "" : "%7C") + latLngArrayList.get(j).latitude + "," + latLngArrayList.get(j).longitude;
}
wayPoints = "&waypoints=" + wayPoints; 

String link="https://www.google.com/maps/dir/?api=1&travelmode=driving"+srcAdd+desAdd+wayPoints;  
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(link));  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");  
startActivity(intent);  

此外,您可以使用dir_action=navigate参数来直接打开逐向导航。

于 2017-10-24T12:29:42.750 回答