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
Comments
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: '© <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: '© <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. |
@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. |
@cleversprocket I'm a little confused by this
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 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. |
@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. |
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>]
.The text was updated successfully, but these errors were encountered: