Reputation: 217
From the google maps API documentation I have got this working:
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
what ive tried to do is replicate this piece of code with my own loop so I can use previously stored co-ords, like so:
var output;
$.getJSON('DisplayMap.php', function(data) {
var output = "[ ";
for (var i in data.b) {
output+= "new google.maps.LatLng" + "(" + data.b[i].Ya + ", " + data.b[i].Za + ")," + " ";
}
output+= "]";
document.getElementById("placeholder").innerHTML=output;
alert(output);
});
output+= "]";
Ive checked the output and it looks exactly as I want it to. however, when I substitute it in like this is doesnt work:
var flightPlanCoordinates = output;
Upvotes: 0
Views: 75
Reputation: 4406
You have to set up a correct javascript Array
instead of a string. Like this:
var output = [];
$.getJSON('DisplayMap.php', function(data) {
for (var i in data.b) {
output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
}
});
Upvotes: 2
Reputation: 78720
In your new code you are building a string instead of an array. You should have something like this instead:
var output = [];
for (var i in data.b) {
output.push(new google.maps.LatLng(data.b[i].Ya, data.b[i].Za));
}
In addition, I am slightly confused by the fact that you create a variable named output
both inside and outside of getJSON
. Keep in mind that getJSON
is asynchronous and you will not be able to use the variable from within getJSON
immediately after it is called.
Upvotes: 3