I am currently working on an angularjs directive that utilizes the Google Maps Direction Service. In my controller, I have a getFunction that watches for changes in a scope variable. Once the scope variable changes, it triggers the calculateAndDisplayRoute function to update the directions. I am attempting to implement the waypoints feature, but I keep encountering the same issue. The waypoints array should consist of objects with "location" and "stopover" fields. I have an array named wypoints containing google.maps.Place objects retrieved from geocoding, totaling 8 waypoints.
The problem I am facing is that the waypoints do not accept a LatLng field in the location section, which is puzzling to me.
Below is the code snippet for 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 message I continuously encounter is Zero_results.
In addition to this issue, when I remove all waypoints, I notice that the directionService.route sometimes returns either Zero_result, provides start and end points at waypoint[0] and waypoint[8], or occasionally gives start and end points at waypoint[0] and another number within the range of 0-8. I suspect this may be due to the asynchronous nature of the call. However, I believe this should not affect the outcome since the waypoints are defined before the function execution.
Any assistance would be greatly appreciated!