Currently, I am facing an issue with the GoogleMaps API v3.0 where I am trying to store a DirectionsResult in my database and then retrieve it later for use on a map. The problem arises when I attempt to restore the saved object by extracting its JSON representation from the database - the re-hydrated object turns out to be just plain JSON without the original methods and functions of its constituent objects. To combat this, I created a correction routine that takes the raw JSON and reconstructs it by rebuilding all the LatLng and LatLngBound objects. However, there seems to be something missing as my corrected object does not fully replicate the functionality of the original; although the two points are displayed on the map, the purple line connecting them is absent.
I would greatly appreciate any suggestions regarding better techniques for serialization/hydration or insights into what might be lacking in my correction routine.
Thank you.
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 DB (I USE ExtJs)
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);
}
}
}