我正在研究通过waypointsGmaps发送到Intent app的功能,以便用户可以通过我发送的自定义waypoints导航到目标

我在嵌入的Google Maps中绘制此路线时,可以看到Circuit route,但是在Gmaps app中看到相同的路线时,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);


电路路线
android - 通过将航路点传递到GoogleMaps android创建电路路线-LMLPHP

没有电路路线
android - 通过将航路点传递到GoogleMaps android创建电路路线-LMLPHP

最佳答案

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

https://developers.google.com/maps/documentation/urls/guide#directions-action

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

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

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获得的路线如下

android - 通过将航路点传递到GoogleMaps android创建电路路线-LMLPHP

这些航点的Google Maps 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=driving

此屏幕快照显示了您使用Google Maps URL所获得的路线

android - 通过将航路点传递到GoogleMaps android创建电路路线-LMLPHP

如您所见,两条路线相同,因此Directions API和Google Maps URL可以按预期工作。我相信您应该更改使用Google Maps URL的意图代码:

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参数直接打开逐行导航。

我希望这有帮助!

10-07 19:34
查看更多