Saving the DirectionsResult object

790 views
Skip to first unread message

Richard

unread,
Oct 25, 2010, 6:31:58 AM10/25/10
to Google Maps JavaScript API v3
Hello,

I am trying to save the DirectionsResult object that has been
dragged. I want to be able to reload it another time and allow the
route to be edited further.

I can save all of the points in the via_waypoint[] array. This allows
be to redraw the route as a polyline but it is no longer draggable.

The documentation says that the DirectionsResult is returned in JSON
format. So I've tried to convert the DirectionsResult to a JSON
string, save and reload that string as a JSON object and pass it to
the DirectionsRenderer. But it doesn't display and no errors are
thrown.

Below is a sample of what I was trying to do.

directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var str = JSON.stringify(response))
var obj= JSON.parse(str)
directionsDisplay.setDirections(obj);
}

I am presuming that this is possible as Google MyMaps provides this
functionality.

Any help would be gratefully appreciated.

Thanks

Chris Broadfoot

unread,
Oct 25, 2010, 1:43:13 PM10/25/10
to google-map...@googlegroups.com
On Mon, Oct 25, 2010 at 3:31 AM, Richard <richar...@gmail.com> wrote:
This allows be to redraw the route as a polyline but it is no longer draggable.

Are you sure?

Does your DirectionsRenderer have draggable: true set?

Can you post a link to your implementation?

Cheers
Chris

JKurtock

unread,
Oct 25, 2010, 3:55:17 PM10/25/10
to Google Maps JavaScript API v3
Richard -

The documentation does say that the directionsResult is in JSON, but
as you found out it is actually an object. After reading your note, I
tried stringify-parse on a directionsResult, and when I did it Firefox
threw "Invalid value for property <position>". So I, too, was unable
to stringify the directionsResult.

But you shouldn't have to do that. Save all the points in the
via_waypoint[] array, and then to reconstruct the route don't just
build a polyline (that would only be point-point by waypoints anyway),
but feed that array back into the directionsService. The
directionsService will build the same directionsResult, because you
are giving it the same waypoints.

There is one gotcha; you are limited to 8 intermediate waypoints, and
you may have dragged in more. But this is easy to work around.
Directions are transitive, so you just need to break up your string of
waypoints into chunks of 8, and then reconstruct the resulting route
in your own directionsRenderer.
Reply all
Reply to author
Forward
0 new messages