首页 文章

带有Waypoints的Google Directions Service返回ZERO_RESULTS

提问于
浏览
0

我目前有一个DirectionsRenderer函数,可以正确地路由我页面上的To和From字段 . 路由完成后,我 grab overview_path,然后根据路径从Fusion Table加载元素 . 完成此操作后,我设置了一个侦听器,寻找'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” . 知道为什么会这样吗?我不确定我形成航点的方式是错还是别的 .

1 回答

  • 1

    一些问题:

    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 将再次开火 .

相关问题