Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programatically change layers in Leaflet?

I don't want to the show the layers control on the map, but I want to put some buttons somewhere else to change between layers. Is this possible to change the layer programmatically?

like image 842
nickponline Avatar asked Dec 07 '13 01:12

nickponline


1 Answers

Suppose you have a map:

var map = L.map('worldmap-map').setView([37.8, -96], 4);

To remove a layer, layer1:

map.removeLayer(layer1)

To remove a control layer, ctrlLayer,

map.removeControl(ctrlLayer)

Or you want to add a layer1 to map:

layer1.addTo(map)

For an example, there is a Leaflet example : http://leafletjs.com/examples/choropleth-example.html

You could use firebug or chrome dev tools to see its source.

like image 154
gongzhitaao Avatar answered Oct 25 '22 18:10

gongzhitaao