I am busy with a script that will make a google maps canvas on my website, with multiple markers. I want that when you click on a marker, a infowindow opens. I have done tha
When dealing with marker clusters this one worked for me.
var infowindow = null;
google.maps.event.addListener(marker, "click", function () {
if (infowindow) {
infowindow.close();
}
var markerMap = this.getMap();
infowindow = this.info;
this.info.open(markerMap, this);
});
infowindow is local variable and window is not available at time of close()
var latlng = new google.maps.LatLng(-34.397, 150.644);
var infowindow = null;
...
google.maps.event.addListener(marker, 'click', function() {
if (infowindow) {
infowindow.close();
}
infowindow = new google.maps.InfoWindow();
...
});
...
I have something like the following
function initMap()
{
//...create new map here
var infowindow;
$('.business').each(function(el){
//...get lat/lng
var position = new google.maps.LatLng(lat, lng);
var content = "contents go here";
var title = "titleText";
var openWindowFn;
var closure = function(content, position){.
openWindowFn = function()
{
if (infowindow)
{
infowindow.close();
}
infowindow = new google.maps.InfoWindow({
position:position,
content:content
});
infowindow.open(map, marker);
}
}(content, position);
var marker = new google.maps.Marker({
position:position,
map:map,
title:title.
});
google.maps.event.addListener(marker, 'click', openWindowFn);
}
}
In my understanding, using a closure like that allows the capturing of variables and their values at the time of function declaration, rather than relying on global variables. So when openWindowFn
is called later, on the first marker for example, the content
and position
variable have the values they did during the first iteration in the each()
function.
I'm not really sure how openWindowFn
has infowindow
in its scope. I'm also not sure I'm doing things right, but it works, even with multiple maps on one page (each map gets one open infowindow).
If anyone has any insights, please comment.
I have been an hour with headache trying to close the infoWindow! My final (and working) option has been closing the infoWindow with a SetTimeout (a few seconds) It's not the best way... but it works easely
marker.addListener('click', function() {
infowindow.setContent(html);
infowindow.open(map, this);
setTimeout(function(){
infowindow.close();
},5000);
});
I have a sample of my code that maybe can help. I had set only one infowindow object at global scope. Then use setContent() to set the content before show it.
let map;
let infowindow;
let dataArr = [
{
pos:{lat: -34.397, lng: 150.644},
content: 'First marker'
},
{
pos:{lat: -34.340, lng: 150.415},
content: 'Second marker'
}
];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
// Set infowindow object to global varible
infowindow = new google.maps.InfoWindow();
loadMarker();
}
function loadMarker(){
dataArr.forEach((obj, i)=>{
let marker = new google.maps.Marker({
position: obj.pos,
map: map
});
marker.addListener('click', function() {
infowindow.close()
infowindow.setContent(`<div> ${obj.content} </div>`)
infowindow.open(map, marker);
});
})
}