How do I programatically change layers in Leaflet?

javascript, leaflet

Solution

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.

Problem

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?

Original source

Related problems