28125

Google Directions Service w/ Waypoints returning ZERO_RESULTS

Question:

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.

Answer1:

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.

Recommend

  • Google Map API V3. Unable to create custom polyline for TRANSIT directions
  • How do I put Utilities.sleep() between function calls?
  • Uncaught exception when calling Google maps directionsService
  • How do you change the color of the dotted line on returned Google map directions
  • extending a class with a generic T
  • How can I get the object in JSON using jquery or javascript
  • Google Maps API v3: routeIndex Not Working?
  • How to implement collision detection on nodes which are in StackPane (in JavaFX)?
  • Operating sytem detection for CSS hacks (aka need a “PC only” CSS hack)
  • InfoWindow showing in wrong place
  • Chrome extension update notification
  • Pubnub subscribe stops receiving messages after some time
  • Drawing a polygon over the entire map
  • Android Google Maps: Polygon or shape that hides entire map
  • Python: Split a String Field into 3 Separate Fields using Lambda
  • how can I compare dates in array to find the earliest one?
  • Getting Latitude Longitude from GoogleMaps in TWebBrowser
  • Google map in top corner left
  • Divide a $1 by 3 and adjusting 1 cent
  • How do I shift the decimal place in Python?
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • req.body is undefined - nodejs
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Volley JsonObjectRequest send headers in GET Request
  • How to get a value (ex: baseURL) in every Karate feature?
  • angularjs unit test when to use $rootScope.$new()
  • How to set/get protobuf's extension field in Go?
  • Submit form in a displaytag pagination
  • Javascript convert timezone issue
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • EntityFramework adding new object to nested object collection