简体   繁体   中英

How do you initialize a google.maps.DirectionsResult from a JSON object returned using the api?

I have an object that was returned using the directions api in a php script. This is the PHP code used to request the directions.

// Fetch and decode JSON string into a PHP object
$json = file_get_contents($url . http_build_query($params));
$data = json_decode($json);

This $data result is passed to the javascript where I would like to render these results using a google.maps.DirectionsRenderer. The renderer requires a DirectionsResult object as a parameter, but I cannot pass the PHP result object. I need to convert that PHP result object into a google.maps.DirectionsResult object, but I don't see how?

This would also relate to similar questions about serializing directions in a db and re-rendering them later.

I´d like to re-create the DirectionsResult object and so, O think we have the same problem here. I´ve found a solution, that´s not as good as it should be but, for now it solves the problem.

for reference, follow the instructions of: Jayapal Chandran at http://vikku.info/programming/google-maps-v3/draggable-directions/saving-draggable-directions-saving-waypoints-google-directions-google-maps-v3.htm

he wrote code for php but the client side is javascript json based so you can use it with almost everything.

it consists in call google service again but passing the waypoints as parameters. I think a new call to google is unessesary, but, for now, that´s the only way. I think.. If someone else knows how to di it withhout recall google, please send-me a note..

hope it helps.

André Sobreiro

The response of the Webservice is not intended to be used with the DirectionsRenderer.

It is possible to parse the Webservice-result and convert particular properties(LatLng's,LatLngBound's and encoded polylines) into the expected objects, but there is at least 1 expected property(which contains the request-options), which may not be converted, because you don't know the expected name of this property(currently it's Wb , tomorrow it may be Bw or something else)

Instead of using the DirectionsRenderer create the Markers, Polylines, InfoWindows etc. on your own, the response of the Webservice contains all the required data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM