Question

Following on from a question I had yesterday:

Google Directions Service w/ Waypoints returning ZERO_RESULTS

Dr. Molle said that the directions_changed listener would fire again. They were correct. It's firing infinitely. I'm wondering if there's a better place to put this listener, OR if there's a way to limit the amount of times it's allowed to fire in a set period of time.

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

        });
    });
 }

EDIT: Should explain myself better. Basically as I'm trying to redraw the route, I'm getting an infinite loop of directions_changed.

Also, for anyone looking at this question, I really don't think the downvote was necessary. I didn't lack research or effort, the waypoints part of the documentation is terrible, and in no example did they attempt to use waypoints via LatLng objects. They only used locations.

Was it helpful?

Solution

When you call route(waypoint), set a flag. When the directions_handler function runs, clear the flag, don't re-render the directions.

  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;
    }
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top