I’m currently using Google’s Directions API, and I stumbled upon an issue.
For some reason, a lot of my calls to directionsService.route() to receive the duration of the itinerary seem to return NO_RESULTS, but they shouldn’t because the addresses are correct (100% sure), they are relatively close to each other so there exists an itinerary, and if I check on google Maps, the routes do work.
I’m also confused cause it seems like the NO_RESULTS is entirely dependent on when I do the API calls, as some itineraries will work at some point during the day and then return NO_RESULTS a couple of minutes later.
I’ve tried changing some addresses, those with no postal codes for example, but it doesn’t seem to be affected by the address for some reason and I implemented a 3 tries method (where if I get NO_RESULTS, it will try again several times), but it did nothing.
Here is the code :
`function calculateRoute(id, origin, destination, travelMode, callback) {
var tries = 0;
var maxTries = 3;
function tryRequest() {
directionsService.route({
origin: origin,
destination: destination,
travelMode: travelMode
}, (response, status) => {
if (status === 'OK') {
callback(response.routes[0].legs[0].duration);
} else if (status === 'ZERO_RESULTS' && tries < maxTries) {
tries++;
console.warn('Retrying ' + id + ' request, attempt ' + tries);
tryRequest();
} else if (status === 'ZERO_RESULTS') {
callback({ text: "0 minutes", value: 0 });
} else {
console.error('Directions for ' + id + ' request failed due to ' + status);
}
});
}
tryRequest();
}`
Thank you to whoever can help me, have a great day !
user26527298 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.