I am retrieving results from a web service. The results are returned as JSON. The JSON looks something like this:
{
"001": {
name: 'Grocery Store',
latitude:30.2669456,
longitude:-97.7427782,
calendar: {
month: 'November'
}
},
"002": {
name: 'Ice Cream',
latitude:30.2669425,
longitude:-97.7427742,
calendar: {
month: 'November'
}
}
}
After this data is retrieved, I am trying to add it to a Google Map. I have successfully initialized my map. In fact, the push pins appear in the correct location. When I hover over each push pin, the title appears correctly. However, when I click the push pin, and the info window appears, it appears in the same location for every push pin. In addition, the content of the info window is the same. I've noticed that it is always the values associated with the last key in json
. For that reason, I suspect it has something to do with closures. At the same time, I'm not sure how to fix this.
What am I doing wrong? Here is my code that is looping through the results:
function dataRetrieved(json) {
for (var place in json) {
var marker = new google.maps.Marker({
map: map,
title: json[place].name,
position: {
lat: json[place].latitude,
lng: json[place].longitude
}
});
marker.info = new google.maps.InfoWindow({
content: '<div>' + json[place].calendar.month + '</div>'
});
google.maps.event.addListener(marker, 'click', function() {
marker.info.open(map, marker);
});
}
}
Thank you for your help!