11
Sorry if this is trivial, but I am new to JS and have been at this problem for a few hours to no avail.
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
var markersArray = [];
var infowindowArray = [];
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map
});
markersArray.push(marker);
}
function addInfowindow(string) {
infowindow = new google.maps.InfoWindow({content: string});
infowindowArray.push(infowindow);
}
function findvenues(latitudelongitude) {
$.get("venuefinder.php", { latlong:latitudelongitude.Ka+","+latitudelongitude.La }, function(data){
var myObject = eval('(' + data + ')');
for(x in myObject.response.venues){
var latlonglocation = new google.maps.LatLng(myObject.response.venues[x].location.lat,myObject.response.venues[x].location.lng);
addMarker(latlonglocation);
addInfowindow(myObject.response.venues[x].name);
google.maps.event.addListener(markersArray[x], 'click', function() {infowindowArray[x].open(map,markersArray[x]);});
console.log(markersArray[x],infowindowArray[x]);
}
});
When findvenues is called, the infowindow click events should be added. When I click the icons, the same infowindow of the last element in the array of infowindows always opens.
However, if I manually enter in the following code, it works:
google.maps.event.addListener(markersArray[0], 'click', function() {infowindowArray[0].open(map,markersArray[0]);});
google.maps.event.addListener(markersArray[1], 'click', function() {infowindowArray[1].open(map,markersArray[1]);});
I need this to be dynamic though... What am