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

Why isn't there map.getLayer()? #3126

Closed
cleversprocket opened this issue Jan 9, 2015 · 4 comments
Closed

Why isn't there map.getLayer()? #3126

cleversprocket opened this issue Jan 9, 2015 · 4 comments

Comments

@cleversprocket
Copy link

I've nested layerGroups within a master layerGroup and I can't figure out how to get the master group without accessing map._layers[<layerGroup_id>].

@patrickarlt
Copy link
Member

You should assign your layer groups to variables so you can keep a reference to them.

I imagine your code probally looks like this right now.

var maps = L.map('map').setView([lat,lng], zoom);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

// create the master layer group
L.layerGroup([
  L.layerGroup([
    // make a bunch of layers
  ]),
  L.layerGroup([
    // make a bunch of layers
  ]),
  L.layerGroup([
    // make a bunch of layers
  ])
]).addTo(map);

What you need to do is this

var maps = L.map('map').setView([lat,lng], zoom);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

// create the master layer group
var masterLayerGroup = L.layerGroup().addTo(map);

// create layer groups
var aLayerGroup = L.layerGroup([
  // create a bunch of layers
]);

masterLayerGroup.addLayer(aLayerGroup);

I would takes a look at the Layer Groups and Layers Control tutorial for more details on doing things like this.

@cleversprocket
Copy link
Author

@patrickarlt I'm actually using leaflet in an AngularJS app using Angular-Leaflet Directive. Where I need the reference to the master layerGroup is when I'm adding markers to a clusterGroup. The code for creating the clusterGroup is outside the code where the layerGroup was created so I don't have access to it. I pass the layerGroup's ID in the clusterGroup's options object which is where I would need map.getLayer(). I could put the layerGroup in the $rootScope but that's adding a large object to the scope which is something I don't want to do.

@patrickarlt
Copy link
Member

@cleversprocket I'm a little confused by this

Where I need the reference to the master layerGroup is when I'm adding markers to a clusterGroup

ClusterGroup does not need a layer group to work. ClusterGroup extend FeatureGroup (which extends LayerGroup) so your ClusterGroup IS a LayerGroup. So I don't see where you would need a reference to your master LayerGroup.

In any case you cant rely on map._layers[<layerGroup_id>] to be consistent. Leaflets IDs are meant to be internal and are unreliable because they are created on the fly every page load. So layer 22 in one page isn't necessarily layer 22 in another page. This is why its best to keep references to your layers and manage it yourself.

Have you tried reaching out to the Angular Leaflet Directive maintainers about this? Since they are creating all the objects internally in their directives it seems like they need to provide a way for you to get a hold of those references since you cant do it the normal way.

@cleversprocket
Copy link
Author

@patrickarlt thank you for the suggestions. I'm adding the clusterGroup to a layerGroup so I can toggle a collection of clusterGroups on and off. I decided to add the layerGroup to Angulars $rootScope and all is well.

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

No branches or pull requests

2 participants