Why won't directionsService.route() execute in this case?
-
21-12-2019 - |
Question
So I am trying to get transit directions from the Google Directions API, storing the steps in an array so I can see which transit lines are being used. I know the service is accepting my API key as it successfully creates the LatLng objects, which wouldn't be happening if my script tag wasn't correct.
However in debugging I have determined that the route() function is simply not executing, or the callback function is failing to execute. I will comment in the results of my debugging:
startPnt = new google.maps.LatLng(32.741480, -96.853803);
endPnt = new google.maps.LatLng(32.921941, -96.735327);
var request = {
origin: startPnt,
destination: endPnt,
travelMode: google.maps.TravelMode.TRANSIT,
provideRouteAlternatives: false //*typo spotted here, was formerly provideRoutesAlternatives.*
};
window.alert(request.origin.lat()); //executed successfully
var stepResults = new Array();
window.alert(stepResults.length); //executed successfully
var dService = new google.maps.DirectionsService();
dService.route(request, function(results, status){
window.alert("The function inside dService.route() is running"); /*did not execute*/
if(status==google.maps.DirectionsStatus.OK) { window.alert("Success"); /*did not execute */ stepResults = results.routes[0].legs[0].steps; /*in original code, this DOM/JSON typo was preventing the callback function from running. The typo had left out "results"*/ }
if(status==google.maps.DirectionsStatus.INVALID_REQUEST) { window.alert("Invalid request."); }
if(status==google.maps.DirectionsStatus.OVER_QUERY_LIMIT) { window.alert("Google has stopped accepting queries from this key for today.");//did not execute }
if(status==google.maps.DirectionsStatus.UNKNOWN_ERROR) { window.alert("Google servers encountered an unexpected error. Try again momentarily."); //did not execute }
else window.alert("Not success."); //did not execute
}
);
This code did work once, but I ran it again after tweaking the data acquisition and route() had quit working. I can't figure out why the dService.route() isn't working, or why the callback function refuses to do anything. I've checked the API console, though, and Google hasn't been receiving any requests with my API key. What's the problem here - could it be my intranet that is blocking the request?
Solution
You have syntax errors. This fiddle works for me
var startPnt = new google.maps.LatLng(32.741480, -96.853803);
var endPnt = new google.maps.LatLng(32.921941, -96.735327);
var request = {
origin: startPnt,
destination: endPnt,
travelMode: google.maps.TravelMode.TRANSIT,
provideRouteAlternatives: false //*typo spotted here, was formerly provideRoutesAlternatives.*
};
window.alert(request.origin.lat()); //executed successfully
var stepResults = new Array();
window.alert(stepResults.length); //executed successfully
var dService = new google.maps.DirectionsService();
dService.route(request, function (results, status) {
window.alert("The function inside dService.route() is running"); /*did not execute*/
if (status == google.maps.DirectionsStatus.OK) {
window.alert("Success"); /*did not execute */
stepResults = results.routes[0].legs[0].steps; /*in original code, this DOM/JSON typo was preventing the callback function from running. The typo had left out "results"*/
} else if (status == google.maps.DirectionsStatus.INVALID_REQUEST) {
window.alert("Invalid request.");
} else if (status == google.maps.DirectionsStatus.OVER_QUERY_LIMIT) {
window.alert("Google has stopped accepting queries from this key for today."); //did not execute
} else if (status == google.maps.DirectionsStatus.UNKNOWN_ERROR) {
window.alert("Google servers encountered an unexpected error. Try again momentarily."); //did not execute
} else window.alert("Not success."); //did not execute
});