I'm trying to Spiderfy my markers and adding listeners to them. The Spidefying works fine that means I get the overlapping markers spiderfied but the problem is that I can't seem to figure how to add listeners to these markers. In other words I have tried the following:
- Putting the
oms.addListener()
in a loop. This way, upon clicking on a marker would open allinfoWindow
s on the same marker which isn't delightful. - Putting the
oms.addListener()
outside the loop like the example on the OMS Git repo. https://github.com/jawj/OverlappingMarkerSpiderfier/blob/gh-pages/demo.html. This way all the markers have the sameinfoWindow
which is the last one from the loop.
This is my code:
var iw = new google.maps.InfoWindow();
var oms = new OverlappingMarkerSpiderfier(carte, {keepSpiderfied:true});
oms.addListener('click', function(marker){
iw.setContent(content);
iw.open(carte, marker);
});
var markers =[];
var bounds = new google.maps.LatLngBounds();
if (response.length != 0) {
for (var i = 0 ; i < response.length; i++) {
var loc = new google.maps.LatLng(response[i].latlong[0], response[i].latlong[1]);
bounds.extend(loc);
var lemarqueur = new google.maps.Marker({
position: loc,
title: response[i].title
});
content = '<table><tr><td><img src="'
+response[i].image+'"/></td><td><p style="font-size: 13px">'
+response[i].title+'</p> <p style="font-size: 10px"><b>Artists:</b> '
+response[i].artist+'<br><b>Date:</b> '+response[i].startDate+'<br>'
+response[i].address.name +' '+response[i].address.street + '<br>'
+response[i].address.postalcode +', '+response[i].address.city +', '
+response[i].address.country
+'<br><a target="_blank" href ='
+response[i].url+'>More info</a></p></td></tr></table>';
oms.addMarker(lemarqueur);
markers.push(lemarqueur);
};
carte.fitBounds(bounds);
var markerCluster = new MarkerClusterer(carte, markers);
markerCluster.setMaxZoom(15);
markerCluster.setGridSize(40);
google.maps.event.addDomListener(window, 'load', initialiser);
So I would like to know where should I put the addListener()
block?
This what I have used before in the loop, using google.maps.event.addListener()
, which worked fine:
google.maps.event.addListener(lemarqueur, 'click', function() {
InfoWindow.open(carte, lemarqueur);
});
I hope the question is clear enough,
Thanks in advance.