React: Leaflet Control

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

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

Before We Begin:

LayerControl

You can now do anything you want to do here.

  1. var MyControl = L.Control.extend({
  2. onAdd: function (map){
  3. //Reference to the map
  4. this._map = map;
  5.  
  6. //This is the container to return so it is available on the map
  7. var container = L.DomUtil.create("div");
  8. return container;
  9. },
  10. onRemove: function(){
  11. //Removes the control
  12. L.Control.prototype.onRemove.call(this);
  13. this._map = null;
  14. }
  15. });

Add Control to Map

The options you add in the class definition are the options that the control gets.

  1. var myControl = new MyControl({ position: "bottomright"});
  2. myControl.addTo(this.map);
Series Navigation<< React: Leaflet LayerGroupReact: Leaflet Icon >>