Google Maps Spiderfying Markers & Adding Listeners

2019-06-08 06:15发布

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 all infoWindows 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 same infoWindow 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.

2条回答
你好瞎i
2楼-- · 2019-06-08 06:56

The oms.addListener() does not act the same way as google.maps.event.addListener(). So I somehow made it to work by using the original google.maps.event.addListener() instead of oms.addListener() inside a for loop. Like so:

var infoWindows = [];

function closeInfoWindows(){
    var i = infoWindows.length;
    while(i--){
        infoWindows[i].close();
    }
}

function newMarker(map, response, oms){
var loc = new google.maps.LatLng(response.latlong[0], response.latlong[1]);
var marker = new google.maps.Marker({
    position: loc,
    title: response.title
});
oms.addMarker(marker);
var WindowOptions = { content:'some desc...'};    
var InfoWindow = new google.maps.InfoWindow(WindowOptions);
infoWindows.push(InfoWindow);
google.maps.event.addListener(marker, 'click', function() {
    closeInfoWindows();
    InfoWindow.open(carte, marker); // or this instead of marker
});
return marker;
}


for (var i = 0 ; i < response.length; i++) {
        markers.push(newMarker(map, response[i], oms));
    };

Extra credit to: geocodezip and All infowindows have same data

A working example could be found here (my app): concert-dacote.com

查看更多
仙女界的扛把子
3楼-- · 2019-06-08 07:02

Here is another approach to adding listeners to your spiderfiered markers with NO FOR LOOP NEEDED:

var oms = new OverlappingMarkerSpiderfier (...) ;

// Create this function
function addClickListenerToMarker(marker, onClickListener) {
    oms.addListener('click', function (markerArg, eventArg) {
        if (marker == markerArg)
            onClickListener(marker, eventArg);
    });
}

// Add a click listener to your marker
var myMarker = ...
addClickListenerToMarker(myMarker, function (marker, event) {
    alert('I have been clicked');
});
查看更多
登录 后发表回答