I have an array with direction renderer json's which are stringified.
array[1].direction = '{\"routes\":[{\"bounds\":\"\....'
array[1].direction = '{\"routes\":[{\"bounds\":\"\....'
.
.
This is what i did to render those json's on my map.
for (var x=2; x<array.length; x++) {
renderDirections(array[x].direction);
}
function renderDirections(result){
var directionRenderer = new google.maps.DirectionsRenderer();
myDirections = JSON.parse(result, function(name, value){
if(/^LatLngPlaceHolder\(/.test(value)) {
var match = /LatLngPlaceHolder\(([^,]+),([^,]+)\)/.exec(value);
return new google.maps.LatLng(match[1], match[2]);
}
else{
return value;
}
});
directionRenderer.setMap(map);
directionRenderer.setDirections(myDirections);
directionRenderer.setOptions({suppressMarkers:true,preserveViewport:true});
}
Doing this gives a "Uncaught SyntaxError: Unexpected token \" error at the JSON.parse line.
However, if i do:
for (var x=2; x<array.length; x++) {
var examplejson = '{\"routes\":[{\"bounds\":\"\....'; //note that its an example json which i logged from `array[x].direction`
renderDirections(examplejson);
}
it renders the json.