I am encountering a very strange error with my project. I normally use the version flag “3”, but today I started getting errors, and rolling back to “3.8” fixes the issue.
In the header of my routing file, I have a couple of variables like so:
var SERVICE = new google.maps.DirectionsService(),
SERVICE_OPTIONS = {
"avoidTolls": false,
"avoidHighways": false,
"optimizeWaypoints": false,
"provideRouteAlternatives": true,
"unitSystem": google.maps.DirectionsUnitSystem.METRIC,
"travelMode": google.maps.DirectionsTravelMode.DRIVING
};
var RENDERER = new google.maps.DirectionsRenderer(),
RENDERER_OPTIONS = {
"draggable": true,
"preserveViewport": false,
"suppressMarkers": false,//true,
"suppressPolylines": false,
"suppressInfoWindows": true,
"polylineOptions": {
"strokeColor": "#9900ff",
"strokeOpacity": 0.5,
"strokeWeight": 4
}
};
And way down near the bottom I have a method to
function route(zoomToResults,mode) {
RENDERER_OPTIONS.preserveViewport = !zoomToResults;
RENDERER.setOptions(RENDERER_OPTIONS); // error is thrown here
/* more code, and then */
SERVICE.route(SERVICE_OPTIONS,parseRouteResults);
};
I have not changed the code, I just noticed I get an error now when I try to use RENDERER.setOptions() before receiving the route results. The error I get is Uncaught TypeError: Cannot read property 'travelMode' of undefined, and is not thrown in 3.8.
Is this a bug with Google’s API or a change to the way they’ve implemented their code internally that does not account for this exception?
I found my answer…
From now on,
DirectionsRenderer.setOptions()must be invoked with aDirectionsResultadded to yourDirectionsRendererOptionsliteral.This throws an error:
This works: