I've been struggling with this one the whole week and all I could find were answers to "map with multiple infowindows" but none that relate to multiple maps on one page, each map having one infowindow. When I click the marker on the first map in the page, the second map's marker infowindow popups up in the second map.
Please see a screenshot of what I mean: Multi maps popup issue
The actual code that handles that is as follows below. It's actually an improved way (at least the last part after line " marker[i] = new google.maps.Marker({ ") of displaying the map with their markers and infowindows after I searched high and low on google (and especially stackoverflow) for any clues that might help.
I have no idea how to make each map's marker display their own infowindow, not the last one (last map's marker). Any clues in that direction would be greatly appreciated, thank you!
var map_collection = Drupal.settings.wysiwyg_map_maps;
var map = [];
var marker = [];
for (var i = 0; i < map_collection.length; i++) {
var latlng = new google.maps.LatLng(map_collection[i]['lat'], map_collection[i]['lon']);
switch(map_collection[i]['map_type']) {
case 'satellite':
var mapOptions = {
zoom: parseInt(map_collection[i]['zoom']),
scrollwheel: false,
center: latlng,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
break;
case 'hybrid':
var mapOptions = {
zoom: parseInt(map_collection[i]['zoom']),
scrollwheel: false,
center: latlng,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.HYBRID
};
break;
case 'terrain':
var mapOptions = {
zoom: parseInt(map_collection[i]['zoom']),
scrollwheel: false,
center: latlng,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
break;
default:
var mapOptions = {
zoom: parseInt(map_collection[i]['zoom']),
scrollwheel: false,
center: latlng,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
break;
}
// Create the map and drop it into the relavent container.
var mapContainer = document.getElementById("wysiwyg_map_" + map_collection[i]['container_id']);
if (mapContainer) {
map[i] = new google.maps.Map(document.getElementById("wysiwyg_map_" + map_collection[i]['container_id']), mapOptions);
// Add marker popup title and text
var displayPopup = 0;
if(map_collection[i]['marker_popup_title'] && map_collection[i]['marker_popup_text']) {
var markerPopupContent = '<div id="marker-content">' +
'<div id="marker-popup-title"><h1>' + map_collection[i]['marker_popup_title'] + '</h1></div>' +
'<div id="marker-popup-text"><p>' + map_collection[i]['marker_popup_text'] + '</p></div>' +
'</div';
displayPopup = 1;
} else if(map_collection[i]['marker_popup_title']) {
var markerPopupContent = '<div id="marker-content">' +
'<div id="marker-popup-title"><h1>' + map_collection[i]['marker_popup_title'] + '</h1></div>' +
'</div';
displayPopup = 1;
} else if(map_collection[i]['marker_popup_text']) {
var markerPopupContent = '<div id="marker-content">' +
'<div id="marker-popup-text"><p>' + map_collection[i]['marker_popup_text'] + '</p></div>' +
'</div';
displayPopup = 1;
}
if(displayPopup) {
var popupBox = new google.maps.InfoWindow({
content: markerPopupContent
});
}
// Add the marker to the map.
marker[i] = new google.maps.Marker({
position: latlng,
map: map[i],
title: map_collection[i]['marker_title'],
animation: google.maps.Animation.DROP
});
if(displayPopup) {
var currentMap = map[i];
var currentMarker = marker[i];
currentMarker.addListener('click', function() {
popupBox.open(currentMap, currentMarker);
});
if(map_collection[i]['currentMarker_popup_default'] == 'true') {
google.maps.event.trigger(currentMarker, 'click');
}
}
}
}