--- layout: api title: "v2.1.5 JavaScript Library: L.LayerGroup" categories: api version: v2.1.5 permalink: /api/v2.1.5/l-layergroup ---

## LayerGroup

Used to group several layers and handle them as one. If you add it to the map, any layers added or removed from the group will be added/removed on the map as well. Implements ILayer interface.

    L.layerGroup([marker1, marker2])
    	.addLayer(polyline)
    	.addTo(map);

### Creation

Factory | Description
--- | ---
`L.LayerGroup( <ILayer[]> layers? )` | Create a layer group, optionally given an initial set of layers.

### Methods

Method | Returns | Description
--- | --- | ---
`addTo( <Map> map )` | `this` | Adds the group of layers to the map.
`addLayer( <ILayer> layer )` | `this` | Adds a given layer to the group.
`removeLayer( <ILayer> layer )` | `this` | Removes a given layer from the group.
`removeLayer( <String> id )` | `this` | Removes a given layer of the given id from the group.
`hasLayer( <ILayer> layer )` | `Boolean` | Returns `true` if the given layer is currently added to the group.
`getLayer( <String> id )` | `ILayer` | Returns the layer with the given id.
`getLayers()` | `Array` | Returns an array of all the layers added to the group.
`clearLayers()` | `this` | Removes all the layers from the group.
`eachLayer( <Function> fn, <Object> context? )` | `this` | Iterates over the layers of the group, optionally specifying context of the iterator function.
    group.eachLayer(function (layer) {
    	layer.bindPopup('Hello');
    });
`toGeoJSON()` | `Object` | Returns a GeoJSON representation of the layer group (GeoJSON FeatureCollection).