I have to obtain route directions with the Google maps v3 WEB (http request) api (computations are made server-side, based on this data, without client interaction, so no JS available). Then, the routes are sorted out with internal logic, and the server (apache/PHP) send a web html/js/jQuery page to the client, which displays a page with a Google map (displayed with JS (ajax) api)
On this map, i would like to render the routes obtained earlier by the server with the web api.
So I tried to give the DirectionsResult to the google.maps.DirectionsRenderer::setDirections() but it does nothing, and firebug doesn't trace any error (and google JS if obfuscated, of course)
Yes, I first set the map with setMap(), and the directionsRenderer is initialized correctly, all that is OK !
It seems that de DirectionsResult is not the same when obtained from Web API and from JS API. Is there a way to "translate" it, so that it could be rendered ?
The alternative would be to do the route computations again on the client side with the ajax api, only to render them, but I think this is stupid to recompute directions as I already have them; so I don't want to do it this way unless it is the only way.
Thank you for your help !
Best Answer
You need to typecast each Object on the JSON response to the proper kind.
The web service API also doesn't return the
ub
property of the DirectionsResult (undocumented property), you need that as well. TheDirectionsRenderer
expectsub.travelMode
,ub.origin
andub.destination
(all strings). Theorigin
anddestination
are only used if you enable thedraggable
property on the DirectionsRendererOptions - they are basically the same value that you pass on theDirectionsService#route()
call.Assuming you are on a browser that supports the ES5
Array#forEach
method:So you would use it like this:
PS: make sure you load the Geometry Library otherwise the
google.maps.geometry.encoding.decodePath
method won't be available.