Answer the question
In order to leave comments, you need to log in
Infowindow Google Maps not working?
// Markers
var jsonMarkers = <?php echo json_encode($our_offices); ?>;
var markers = [];
var markersPopup = [];
var markersPopupContent = [];
for (var i = 0; i < jsonMarkers.length; i++) {
markers[i] = new google.maps.Marker({
position: { lat: parseFloat(jsonMarkers[i][0].lat), lng: parseFloat(jsonMarkers[i][0].lng) },
title: jsonMarkers[i][0].title,
map: map,
});
markersPopupContent[i] = jsonMarkers[i][0].title;
markersPopup[i] = new google.maps.InfoWindow({
content: 'dwadawdawdwa',
});
markers[i].addListener('click', function() {
markersPopup[i].open(map, markers[i]);
});
};
Answer the question
In order to leave comments, you need to log in
At the time of the click , the variable i will be equal to jsonMarkers.length and therefore markersPopup[i] will be undefined (indices in the array start from 0). A closure like this can save you
markers[i].addListener('click', (function(i) {
return function() {
markersPopup[i].open(map, markers[i]);
};
})(i));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question