谷歌路线服务w /航点返回ZERO_RESULTS [英] Google Directions Service w/ Waypoints returning ZERO_RESULTS

查看:275
本文介绍了谷歌路线服务w /航点返回ZERO_RESULTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个DirectionsRenderer函数,可以在我的页面上正确路由收件人和发件人字段。路由完成后,我抓取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航点);
});

一旦我将它传递回route()函数(该函数与To和如果(航点){

var请求

  = {
origin:document.getElementById(search-input-from)。value,
destination:document.getElementById(search-input-to)。value,
waypoints:航点,
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 ){

//做东西

}
其他{
alert(Directions queries failed:+ status);
}
};

不幸的是,我回来的是路线查询失败:ZERO_RESULTS 。任何想法为什么会发生这种情况?我不知道我是如何形成航点的方式是错误的或其他的。

有些问题:
$ b

  wypnt.toString()。split(,); 

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

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

为什么是ar您在这里转换经度和纬度?

  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 会再次触发。


I currently have a DirectionsRenderer function that properly routes To and From fields on my page. After the routing is done I grab the overview_path and then load elements from a Fusion Table based on the path. Once this is done I've set up a listener looking for 'directions_changed', which would indicate a waypoint as such:

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);
    });

Once I pass it back to the route() function (the function that works normally with the To and From fields), I have this section of code:

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
    };
}

The rest of the code is based off the following if statement:

directionService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {

       //do stuff

     }
    else {
                alert("Directions query failed: " + status);
            }
    };

Unfortunately all I'm getting back is "Directions query failed: ZERO_RESULTS". Any idea why this is happening? I'm not sure if the way I'm forming the waypoint is wrong or something else.

解决方案

Some issues:

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

this will not have any effect to wypnt, split will not modify the original object. it has to be:

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

Why are you switching latitude and longitude here?

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

it has to be

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

Most of all: why do you do it at all? you take an array, convert it to a string, split the string to get the original array.

Simply use:

 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);
});

But note: when you redraw the route directions_changed will fire again.

这篇关于谷歌路线服务w /航点返回ZERO_RESULTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆