继昨天的一个问题之后:
莫勒博士说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 对象使用航点。他们只使用位置。