React: Leaflet LayerGroup

This entry is part 6 of 9 in the series React: Leaflet
(Last Updated On: )

In this tutorial I will demonstrate how to add a layer group control. Refer to the documentation for more information.

Before We Begin:

LayerGroup

You can now do anything you want to do here.

  1. var MyLayerGroup = L.LayerGroup.extend({
  2. onAdd: function(map) {
  3. //Reference to the map
  4. this._map = map;
  5. },
  6. onRemove: function(){
  7. //Removes the layer
  8. L.LayerGroup.prototype.onRemove.call(this, map);
  9. },
  10. initialize: function (options) {
  11. L.LayerGroup.prototype.initialize.call(this);
  12. //The options sent in from initialisation
  13. L.Util.setOptions(this, options);
  14. }
  15. });

Add LayerGroup to Map

  1. var myLayerGroup = new MyLayerGroup();
  2. myLayerGroup.addTo(this.map);

Overlay

If you want to add the layergroup to overlays menu.

  1. this.layerControl.addOverlay(myLayerGroup, "My Layer Group");
Series Navigation<< React: Leaflet DomEventReact: Leaflet Control >>

One thought on “React: Leaflet LayerGroup”

Comments are closed.