Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasLayer() to the markercluster object #44

Closed
grantlucas opened this issue Aug 23, 2012 · 17 comments
Closed

Add hasLayer() to the markercluster object #44

grantlucas opened this issue Aug 23, 2012 · 17 comments
Assignees

Comments

@grantlucas
Copy link

When adding new markers to a map, it'd be great to be able to easily check if it already has that layer. It's pretty simple to do without this function but it would be a nice added feature.

@Adventsparky
Copy link

+1, This would be nice.

@danzel
Copy link
Member

danzel commented Aug 26, 2012

How about if calling addLayer when the layer was already added it didn't re-add the same marker?

@grantlucas
Copy link
Author

Hm. I can see how that would be good but I can also see cases where you just want to check if that layer is there. I would be more for the hasLayer method to keep it similar to what the normal map object has. I also wonder if there may be some reason to add a duplicate layer.

@danzel
Copy link
Member

danzel commented Aug 27, 2012

Yep fair enough. Just a bit hard in the code ATM as we don't keep a list of all markers around to easily check against, so we'll need to traverse the tree.

I don't think you should be adding the same marker to the map twice. Currently if you do that with the clusterer it clusters with it self and results in all sorts of weirdness.

@grantlucas
Copy link
Author

Ah true true about duplicate markers.

Right now, my duplicate detection is working like so. (pretty much ripped it from hasLayer on the map object).

for(var id in me.markers)
{   
    var layerId = L.Util.stamp(me.markers[id].marker);
    if(!me.markersCluster._layers.hasOwnProperty(layerId))
        me.markersCluster.addLayer(me.markers[id].marker);
}

Seems to be working alright so far.

@danzel
Copy link
Member

danzel commented Aug 27, 2012

I think that will only work if the given marker is currently visible on the map.
If it is inside a cluster at the visible zoom level then it will not be in _layers so you'll add it again.

@grantlucas
Copy link
Author

Ah that makes sense. I just checked over my code and in a host of changes in the past week, I'm reseting the entire cluster markering since I'm loading markers through AJAX while moving around the map so my check doesn't even make sense anyways. I had added it at one point when I wasn't resetting the layer.

@ghost ghost assigned danzel Aug 30, 2012
@Adventsparky
Copy link

Any plans to incorporate the check if a layer exists when adding as mentioned? That alone would be nice. the hasLayer() would be great too, but if the check when adding is easier, that's good too.

@danzel
Copy link
Member

danzel commented Aug 30, 2012

Yep, might be looking at this later today, we'll see how my day unfolds.

Out of personal interest, what sort of projects are you guys using the markercluster on?
If it is something private feel free to send me an email (its on my github profile) or if it is something public it would be nice to be able to link to it to show that people are actually getting good use of the project :-)
Thanks!

danzel added a commit that referenced this issue Sep 2, 2012
@danzel
Copy link
Member

danzel commented Sep 2, 2012

Alrighty! I've added this and also made addLayer check if the layer is already in the cluster before adding it.
You can disable the test for a small performance boost if you know you'll only ever add markers that aren't in the clusterer.
The test only runs once the MarkerClusterGroup has been added to the map, so initial load performance will still be the same.
Enjoy!

@danzel danzel closed this as completed Sep 2, 2012
@cvisto
Copy link

cvisto commented Oct 30, 2012

Hello. In my application sometime I need to readd markers that were removed from clusterGroup. hasLayer(marker) method returns "true" if marker was removed from cluster. I think it's because at that moment marker.__parent._group === clusterGroup. To my mind it would be better if hasLayer returns correct answer for remove markers (doesn't metter were they removed by removeLayer[s] or clearLayers).

What do you think about that?

@cvisto
Copy link

cvisto commented Oct 30, 2012

Now there are 4 ways of adding layers to clusterGroup:

  1. addLayer() when cluster IS NOT on the map
  2. addLayers() when cluster IS NOT on the map
  3. addLayer() when cluster IS on the map
  4. addLayers() when cluster IS on the map

Now only one of them (3rd) checks for duplicates. Maybe you can make all of them do that checking. And add some MarkerClusterGroup option (for example, "checkDuplicates: true") to allow developer to decide if he wants that checking (with understanding that it will decresase productivity) or not (in order to increase performance)

@danzel
Copy link
Member

danzel commented Oct 31, 2012

I refactored how hasLayer works so that it is super efficient and we always (or at least should always) be testing it before adding a layer to the map.
I'll take a double check through the code to make sure that everything is testing for duplicates and that hasLayer works correctly after removing the layer.

@danzel
Copy link
Member

danzel commented Oct 31, 2012

Thanks for the report, have fixed up the issue now.
Also, you should post bugs in new open bugs, not in old closed bugs, people will miss them!

@cvisto
Copy link

cvisto commented Nov 1, 2012

Great! Thanks for hasLayer. It works now for previously removed layers.
And I will take into account about new open bugs.

As far as I understood you didn't include yet dups testing into adding layers process when cluster group is not on the map, did you?

@danzel
Copy link
Member

danzel commented Nov 7, 2012

Will take a look, that does sound like something I may have missed, thanks :)

danzel added a commit that referenced this issue Nov 9, 2012
…es adding the same layer multiple times before adding the MarkerClusterGroup to the map. Refs #44
@danzel
Copy link
Member

danzel commented Nov 9, 2012

@cvisto yep, was a bug, fixed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants