I'm using Google Maps API to generate a Map with a few markers, each one has a infowindow.
All of the neccessary details (lattitude, longtitude) are printed in the HTML, I used JS loop in order to get those values in order to create the markers and the infowindow.
The map generates fine but when I click on a markers I get "Uncaught TypeError: Cannot read property 'open' of undefined" when it's supposed to open an infowindow
HTML:
<select>
<option class="markerobject" data-title="Headquarters" data-address="Suite 707, 2 Huntley St Alexandria NSW 2015" data-latitude="-33.911063" data-longitude="151.193590">Headquarters</option>
<option class="markerobject" data-title="Melbourne" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Melbourne</option>
<option class="markerobject" data-title="Brisbane" data-address="Myer Centre Brisbane Shop 112 (Next to shaver shop) Brisbane QLD 4000" data-latitude="-27.466099" data-longitude="153.023588">Brisbane</option>
<option class="markerobject" data-title="Carindale" data-address="Westfield, Carindale Ground level, Kiosk 112 Carindale QLD 4152" data-latitude="-27.505068" data-longitude="153.101718">Carindale</option>
</select>
Javascript:
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('allstores'), mapOptions);
var fonekingicon = document.getElementById("mappointer");
var fonekingiconsrc = fonekingicon.getAttribute("src");
var markerObjects = document.getElementsByClassName("markerobject");
var markers = [];
var infowindows = [];
for (var i = 0; i < markerObjects.length; ++i) {
infowindows[i] = new google.maps.InfoWindow({
content: markerObjects[i].getAttribute("title")
});
markers[i] = new google.maps.Marker({
position: new google.maps.LatLng(
markerObjects[i].getAttribute("data-latitude")
,
markerObjects[i].getAttribute("data-longitude")
),
map: map,
icon: fonekingiconsrc
});
google.maps.event.addListener(markers[i], 'click', function() {
infowindows[i].open(map,markers[i]);
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);