Cant Deserialize DirectionsResult Objects

491 views
Skip to first unread message

sisdog

unread,
Dec 29, 2010, 1:13:08 PM12/29/10
to google-map...@googlegroups.com
Hi all.
I'm trying to save a DirectionsResult to my database and then retrieve it later to use on a map.  My problem is that when I try to re-hydrate the saved object by pulling its JSON representation from my database, the object is just dumb JSON, it doesn't have the original methods and functions of its constituent objects.  So, I built a fix routine that takes the dumb JSON and rebuilds it by reconstructing all the LatLng and LatLngBound objects. But, something is still missing because my fixed object doesn't work like the original, the two points show up on my map but the purple line between them is missing.
I'd appreciate advice on either a better technique for serialization/hydration or any ideas as to what my fix routine might be missing.
Thanks.

li

******** CODE ********
request = {
origin: homeLocation, 
destination: jobLocation,
travelMode: google.maps.
DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var str = Ext.encode(response); //<<==SAVING RAW JSON OBJECT TO DATABASE
var z = eval('(' + str + ')'); //<<==REHYDRATING DirectionsResult RAW JSON OBJECT
FixDirectionResult(z); //<<==ATTEMPT TO RE-ESTABLISH ORIGINAL OBJECTS
directionsRenderer.
setDirections(z); //<<==THIS WORKS WITH response BUT NOT WITH z
}
);
function FixDirectionResult(rslt) {
for(r=0; r<rslt.routes.length; r++) {
var route = rslt.routes[r];
var bounds = route.bounds;
route.bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(bounds.U.b,bounds.O.d), 
new google.maps.LatLng(bounds.U.d,bounds.O.b));

for(l=0; l<route.legs.length;l++) {
var leg = route.legs[l];
leg.start_location = new google.maps.LatLng(leg.start_location.wa,leg.start_location.ya);
leg.end_location = new google.maps.LatLng(leg.end_location.wa,leg.end_location.ya);

for(s=0; s<leg.steps.length;s++) {
var step = leg.steps[s];
step.start_location = 
new google.maps.LatLng(step.start_location.wa,step.start_location.ya);
step.end_location = 
new google.maps.LatLng(step.end_location.wa,step.end_location.ya);

for(p=0;p<step.path.length;p++) {
var path=step.path[p];
step.path[p] = new google.maps.LatLng(step.path.wa,step.path.ya);
}
}
}

for(o=0; o<route.overview_path.length;o++) {
var overview = route.overview_path[o];
route.overview_path[o] = new google.maps.LatLng(overview.wa,overview.ya);
}
}

Rossko

unread,
Dec 29, 2010, 3:48:35 PM12/29/10
to Google Maps JavaScript API v3
> I'm trying to save a DirectionsResult to my database and then retrieve it later to use on a map.

You could look to see if anyone has tried this before
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/8cedc5ad11d9f034/9920f116510eaf46

sisdog

unread,
Dec 30, 2010, 4:48:50 PM12/30/10
to google-map...@googlegroups.com
Hey Rossko,
First off, thanks for your reply.  I'm not sure if you're being sarcastic and insinuating that I haven't done any research but yes, 
I've searched other posts looking for solutions.  The link you provided doesn't address my
goal which is to NOT repeatedly call google for information I've already retrieved once.  The
 link you suggested recommends calling the service again which defeats the point.  Plus, I don't
want to have to deconstruct the response into my own new array of objects which will removes
some of the the original semantics of the complete response object (like English directions, alternate routes, etc).

that said that he successfully employed the same strategy as the one I'm using.  And yes, I've sent him a query asking for his technique.
But, it's not working and I'm just wondering how others have tried to solve this problem in general.

Again, thx for your help.
Mark


Rossko

unread,
Dec 30, 2010, 7:06:50 PM12/30/10
to Google Maps JavaScript API v3
Have you considered using the HTTP Directions service, which would
return more easily digested results?
http://code.google.com/apis/maps/documentation/directions/
Reply all
Reply to author
Forward
0 new messages