My application plots a bunch of markers on a map read from a MySQL database, and I want to display some content in each of the infowindows. My issue is the infowindows will only display the content of the last entry in my database. Below is my code, where I simply try to display the name of the location. I've tried alerting the variables to see if they are indeed displaying the correct content, which they are. I realize it is a javascript closure problem, but I don't know how to go about fixing this.
var startLat;
var startLng;
var locationName;
<?php foreach($workstations as $workstation):?>
startLat = "<?php echo $workstation['lat']?>";
startLng = "<?php echo $workstation['lng']?>";
locationName = "<?php echo $workstation['name']?>";
var marker = new google.maps.Marker({
position: new google.maps.LatLng(startLat, startLng),
map: map,
icon: "images/purple.png"
});
//load all the infowindow content
var contentString = [
'<div id="InfoText" style="margin: 0px; padding: 0px; overflow: hidden; border:0px">',
'<div class="tabs"><ul><li><a href="#tab1">General</a></li>',
'<li><a href="#tab2">General 2</a></li></ul>',
'<div id="tab1">',
'<b>' + locationName + '</b> - ' + locationName + '<BR>',
'<input id="save" type="submit" value="Reserve Today" onclick="saveStation(addMarker)" />',
'</div>',
'<div id="tab2">',
'</div>',
'</div>'
].join('');
//alert(contentString);
var infowindow = new google.maps.InfoWindow({content: contentString});
new google.maps.event.addListener(marker, 'click', (function(marker) {
return function(){
infowindow.open(map, marker);
}
})(marker));
<?php endforeach;?>