Reputation: 356
I am creating an angularjs directive with the google maps direction service. What I am doing is performing a getFunction in my controller and scopeing a watch variable. Once the scope variable changes, I update my directions with the calculateAndDisplayRoute function. I am trying to use the waypoints feature but keep getting boggled down on the same issue. Waypoints array is supposed to be specified as an array of objects containing the fields "location" and "stopover". I have a google.maps.Place object with the retrieved from geocoding in an array named wypoints. I currently have waypoints array of 8.
What I am having trouble with is that the waypoints don't accept a LatLng field in the location section which I am really confused about.
The following is my calculateandDisplayRoute function:
function calculateAndDisplayRoute(directionsService, directionsDisplay, waypoints) {
var waypts = [];
for (var i = 0; i < waypoints.length; i++) {
waypts.push({
location: waypoints[i].geometry.location,
stopover: true
});
}
directionsService.route({
origin: waypts[0].location,
destination: waypts[waypts.length - 1].location,
waypoints: waypts,
travelMode: google.maps.TravelMode.DRIVING
}, function (response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
The error I keep getting is that I get Zero_results.
While I am at it, I also have another issue which if I remove all waypoints, I notice that the directionService.route sometimes gives me either the Zero_result, Gives me start and end of points at waypoint[0] and waypoint[8], or lastly sometimes gives me start and end points of waypoint[0] and waypoint[//some number between 0-8]. I am assuming this is because this is anasynchronous call? I don't think it should matter since the waypoints are being defined prior to the function even running.
Thanks for anyhelp!
Upvotes: 2
Views: 970
Reputation: 333
Updated fiddle showing the route for the points provided - http://jsfiddle.net/af6f7su0/140/
The reason why you get zero results is because directions for the region you provided is not available. The documentation says
"ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address
I find the fiddle you provided has duplicate placeIds and that would cause the Direction Service API to return zero results. I removed one of the duplicate placeIds in the updated fiddle. Also, the location geometry of the placeIds provided is somehow showing up in the wrong place and I modified the fiddle to use formatted_address
instead of location.geometry
. Ensure that all points fall in a land area. Also, you have included all of the points as the waypoints
property, but only the intermediate points (except the first and last one) should be a valid value for this property. Here's the updated calculateAndDisplayRoute
function:
function calculateAndDisplayRoute(directionsService, directionsDisplay, waypoints) {
var waypts = [];
var intermediatePoints = [];
for (var i = 0; i < waypoints.length; i++) {
waypts.push({
location: waypoints[i].formatted_address,
stopover: true
});
}
intermediatePoints = waypts.slice(1, -1);
directionsService.route({
origin: waypts[0].location,
destination: waypts[waypts.length - 1].location,
waypoints: intermediatePoints,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
Hope this helps. Let me know if not.
Upvotes: 1