继昨天的问题之后:
Google Directions Service w/ Waypoints returning ZERO_RESULTS
博士。莫尔说,directions_changed
听众会再次开火。他们是对的。它无限地射击。我想知道是否有更好的地方放置这个听众,或者是否有办法限制允许在一段时间内开火的次数。
function route(waypoint) {
distance = 5; // Default distance
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
};
}
var directionRendererOptions = { draggable: true };
// Make the directions request
directionService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setOptions(directionRendererOptions);
directionsDisplay.setDirections(result);
directionsDisplay.setMap(map);
var path = result.routes[0].overview_path;
// more code here that won't matter
}
else {
alert("Directions query failed: " + status);
}
//listener for dragged route/polyline
google.maps.event.addListener(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
的无限循环。
另外,对于任何看过这个问题的人,我真的不认为这是必要的。我没有缺乏研究或努力,文档的航点部分很糟糕,并且没有例子他们尝试通过LatLng对象使用航点。他们只使用了位置。
答案 0 :(得分:1)
当你呼叫路线(航路点)时,设置一个标志。当directions_handler函数运行时,清除标志,不要重新渲染方向。
var directionsRedraw = false;
google.maps.event.addListener(directionsDisplay, 'directions_changed', function(){
if (directionsRedraw == false) {
directionsRedraw = true;
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);
} else {
directionsRedraw = false;
}
});