谷歌方向服务w/Waypoints返回ZERO_RESULTS

Google Directions Service w/ Waypoints returning ZERO_RESULTS

本文关键字:返回 ZERO RESULTS Waypoints 方向 服务 谷歌      更新时间:2023-09-26

我目前有一个DirectionsRenderer函数,可以正确路由页面上的To和From字段。路由完成后,我获取overview_path,然后根据路径从Fusion表加载元素。完成后,我设置了一个侦听器来查找"directions_changed",这将指示一个航路点:

google.maps.event.addListener(directionsDisplay, 'directions_changed', function(){
        var wypnt = directionsDisplay.getDirections().routes[0].legs[0].via_waypoints.toString().match(/[^()]+/);
        wypnt.toString().split(",");
        wypnt = new google.maps.LatLng(wypnt[1],wypnt[0]);
        var waypoint = [];
        waypoint.push({ location: wypnt, stopover: true });
        route(waypoint);
    });

一旦我将其传递回route()函数(该函数通常与to和From字段一起工作),我就有了以下代码段:

if(waypoint){
    var request = {
        origin: document.getElementById("search-input-from").value,
        destination: document.getElementById("search-input-to").value,
        waypoints: waypoint,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
}
else{
    var request = {
        origin: document.getElementById("search-input-from").value,
        destination: document.getElementById("search-input-to").value,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
}

代码的其余部分基于以下if语句:

directionService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
       //do stuff
     }
    else {
                alert("Directions query failed: " + status);
            }
    };

不幸的是,我得到的只是"方向查询失败:ZERO_RESULTS"。知道为什么会发生这种事吗?我不确定我形成航路点的方式是错误的还是其他什么。

一些问题:

    wypnt.toString().split(",");

这对wypnt没有任何影响,split不会修改原始对象。必须是:

     wypnt = wypnt.toString().split(",");

你为什么在这里切换纬度和经度?

    wypnt = new google.maps.LatLng(wypnt[1],wypnt[0]);

必须是

   wypnt = new google.maps.LatLng(wypnt[0],wypnt[1]);

最重要的是:你为什么要这么做?取一个数组,将其转换为字符串,拆分字符串以获得原始数组。

简单使用:

 google.maps.event.addListenerOnce(directionsDisplay, 'directions_changed', 
   function(){
   var waypoints=directionsDisplay.getDirections().routes[0]
                    .legs[0].via_waypoints||[];
    for(var i=0;i<waypoints.length;++i){
       waypoints[i]={stopover:true,location: waypoints[i]}
    }
    route(waypoints);
});

但请注意:当您重新绘制路线时,directions_changed将再次激发。