L.Control.Layers - possible to turn layers on and off with JavaScript

9,577 views
Skip to first unread message

Bjørn Sandvik

unread,
Oct 27, 2012, 7:55:16 AM10/27/12
to leafl...@googlegroups.com
Hi, 

If I add a new layer to the layer control, how can I switch it on with JavaScript? 

Bjørn

Henrik Rosvall

unread,
Jan 19, 2013, 10:09:22 AM1/19/13
to leafl...@googlegroups.com
Trying to do the same! Have you succeeded?

Bryan McBride

unread,
Jan 19, 2013, 10:54:39 PM1/19/13
to leafl...@googlegroups.com
Just call map.removeLayer(yourLayer). You can try it on the Layers Control Example here: http://leafletjs.com/examples/layers-control-example.html. Call map.removeLayer(cities) in your console.

BRYAN

Henrik Rosvall

unread,
Jan 20, 2013, 3:00:58 PM1/20/13
to leafl...@googlegroups.com
I get it working with:  map.addLayer(overl); and map.removeLayer(overl);

And for updating the layer control I called this: layersControl._update(); 

And layersControl is:

var layersControl = new L.Control.Layers( {'Skoterleder.org OSM':skoterleder }, {'Visa skoterleder':overl, 'Visa information':info });

Now it's working just as I like it!

Tu Tran

unread,
May 20, 2013, 2:12:06 PM5/20/13
to leafl...@googlegroups.com
Hi,  

I'm trying to update my layers control but I can't figure it out.  It seems like you succeeded.  

Currently, I have a layer control as

L.control.layers(baseLayers,overlays).addTo(map);

After running several lines of code, the overlays layers are updated. I would like to update the layer control.

do I just say, 

L.control.layers(baseLayers,overlays)._update();

Any help is greatly appreciated.

thanks,

Bill Pelon

unread,
May 20, 2013, 6:58:15 PM5/20/13
to leafl...@googlegroups.com
I'm also trying to do a similar thing and have been unsuccessful.

I an trying to use buttons external to the map itself to toggle layers inside the map without using a layer control.

If anyone can point in the right direction I'd appreciate it.

Tu Tran

unread,
May 21, 2013, 8:09:21 PM5/21/13
to leafl...@googlegroups.com

Got it. Thanks

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "Leaflet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/leaflet-js/xvZ1V6shL8M/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to leaflet-js+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Riccardo Klinger

unread,
Jun 9, 2013, 4:19:42 PM6/9/13
to leafl...@googlegroups.com
can you explain how?!

Bryan McBride

unread,
Jun 9, 2013, 4:22:35 PM6/9/13
to leafl...@googlegroups.com

To add a layer: map.addLayer(theLayer).

To remove a layer: map.removeLayer(theLayer).

Bryan R. McBride, GISP
bryanmcbride.com

Riccardo Klinger

unread,
Jun 9, 2013, 4:36:23 PM6/9/13
to leafl...@googlegroups.com
thanks bryan,

i was using:

var dir = L.layerGroup(direction);
map.on( "zoomend", function( e ) {
if (map.getZoom()>14) {
map.addLayer(dir);
}
else if (map.getZoom()<15) {
map.removeLayer(dir)
};
});


where direction is type L.geoJson. wasnt working. So now I am just switching visibility in the map and keep the layer control untouched:

var dir = L.layerGroup();
map.on( "zoomend", function( e ) {
if (map.getZoom()>14) {
dir.addLayer(directions);
dir.addTo(map);
}
else if (map.getZoom()<15) {
dir.clearLayers()
};
});

and it works like a charm!
Reply all
Reply to author
Forward
0 new messages