jawj / OverlappingMarkerSpiderfier-Leaflet

Deals with overlapping markers in the Leaflet maps API, Google Earth-style
253 stars 68 forks source link

Not working correctly with circleMarker #13

Open 96187 opened 10 years ago

96187 commented 10 years ago

I tried to use this with circleMarker but it doesn't seem to work as expected. Instead of expanding the entire group, it only expands one marker per click.

I managed to reproduce the behaviour with the demo by changing var marker = new L.Marker(loc, {icon: new darkIcon()}); to var marker = new L.circleMarker(loc, { color: 'red', radius: 5 });

therealtakeshi commented 10 years ago

Running into this same issue as well. I have multiple maps, one using regular Marker with icons, the other using circleMarker. The Marker one will expand all nearby markers as expected, while the circleMarker expands only 1 per click.

therealtakeshi commented 10 years ago

cc @96187:

This looks like it's related to the marker.setZIndexOffset() method used on lines 296 and 326. In my local version, I've gotten the expected results on a group using circleMarker by adding a simple IF check for marker._path (since circleMarker extends Path, not Marker). See below.

l296: if (marker._path) { marker.bringToFront(); } else { marker.setZIndexOffset(1000000); }

l326: if (marker._path) { marker.bringToBack(); } else { marker.setZIndexOffset(0); }

Greigrm commented 9 years ago

Its a long time since there was activity on this project and while I'm converting my other work to Leaflet 1.0 I thought I'd have a look at this issue. I know nothing about coffescript, so cut/paste it into a website to convert to js and am using that directly now, so all of the below is in the raw js.

As @therealtakeshi above pointed out, when using a circlemarker the zIndexOffset function doesn't apply. I also replaced the accessing of the private variable (_path) by using a 'marker instanceof L.CircleMarker' comparison. That fixed the script error, but now the circle markers simply didn't spiderfy - nothing happened at all. I got firebug into action and stepped through the code and could see that the markers were actually spiderfying and then immediately unspiderfying. It looks as is the click event on the marker was correctly spiderfying, but then the click event was propagating through to the map itself, which caused the normal unspiderfy action called as if the map was clicked in "open space".

To fix it I did a quick hack - there is probably a more elegant solution to this which stops the propagation of the leaflet event, but with my deadlines I'm all about the quick hack.

What I did: 1) add a private variable in the class definition, simply

     this._stopUnspider = false; 

2) find the existing line marker.setZIndexOffset(1000000); replace with

   if (marker instanceof L.CircleMarker) 
         {this._stopUnspider = true;} 
   else {marker.setZIndexOffset(1000000);}

3) find the existing line marker.setZIndexOffset(0); replace with

       if (!marker instanceof L.CircleMarker) { marker.setZIndexOffset(0); }

4) finally, in the unspiderfy function, find this:

   if (this.spiderfied == null) {
      return this;
    }

replace with:

   if (this.spiderfied == null || this._stopUnspider) {
      this._stopUnspider = false;
      return this;
    }

I hope that helps someone else. Sorry for not doing the "proper" update through github, but I don't do coffeescript, and it looks as if this plugin has been abandoned anyway.