React: Leaflet Markers

This entry is part 9 of 9 in the series React: Leaflet

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

Before We Begin:

Text:

LayerGroup onAdd Method

  1. //Define the marker. Since this is text based their is no icon but instead a divIcon
  2. this.textMarker = L.marker([20.5, -0.09],{
  3. icon: L.divIcon({
  4. iconSize: [100,16],
  5. iconAnchor: [22, 30],
  6. className: "",
  7. }),
  8. zIndexOffset: 75,
  9. });
  10.  
  11. //Sets the text
  12. this.textMarker.options.icon.options.html = "This is my text";
  13.  
  14. //Adds the text marker to the layer
  15. this.addLayer(this.textMarker);

LayerGroup onRemove Method

  1. //Remove the text marker you just added
  2. this.removeLayer(this.textMarker);

Results: Now you will see as you turn the layer on and off from the context menu the text will show or hide.

Icon:

Import Image:

  1. import myImagefrom "../images/myImage.png";

Create Icon/Marker onAdd Method

  1. //Define the icon you will be using
  2. var myIcon = L.icon({
  3. iconUrl: myImage,
  4. iconSize: [75, 75], // size of the icon
  5. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  6. });
  7.  
  8. //Define the icon based marker
  9. this.marker = L.marker([20.5, -40.09],{
  10. icon: myIcon,
  11. opacity: 0.7,
  12. zIndexOffset: 30
  13. });
  14.  
  15. //Adds the icon marker to the layer
  16. this.addLayer(this.marker);

onRemove Method

  1. //Remove the marker you just added
  2. this.removeLayer(this.marker);

Results: Now you will see as you turn the layer on and off from the context menu the icon will show or hide.

Set Latitude/Longitude:

You can set the latitude and longitude in one of two ways.

  1. //Set the latitude and longitude
  2.  
  3. //Method 1:
  4. L.marker([20.5, -0.09])
  5.  
  6. //Method 2:
  7. this.textMarker.setLatLng([20.5, -0.09]);

Event(s):

onClick

  1. marker.on("click", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOver

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

MouseOut

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Do my work here
  4. }.bind(this));

Popup:

BindPopup

  1. marker.bindPopup(L.popup(),{
  2. offset: L.point(0,-10) //You can add an offset if you want to
  3. });

OpenPopup

Let’s say you want to open the popup during a mouseover event.

  1. marker.on("mouseover", function(e) {
  2. var marker = e.target;
  3.  
  4. //Set the content to display in the popup
  5. marker.setPopupContent("My Text");
  6. //Now open the popup
  7. marker.openPopup();
  8. }.bind(this));

ClosePopup

Let’s say you want to open the popup during a mouseout event.

  1. marker.on("mouseout", function(e) {
  2. var marker = e.target;
  3. //Close the popup now
  4. marker.closePopup();
  5. }.bind(this));

React: Leaflet Icon

This entry is part 8 of 9 in the series React: Leaflet

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

Before We Begin:

Create Folders/Files:

  • app
    • folder: leaflet
      • folder: images
        • file: yourimage.png

Import Image(s):

  1. import yourimage from "../images/yourimage.png";

Create Icon:

  1. var myIcon = L.icon({
  2. iconUrl: plusimg,
  3. iconSize: [75, 75], // size of the icon
  4. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  5. });

Add to Map:

[51.5, -0.09] refers to the position on the map you want to add it to. Refer to the marker tutorial for further information on markers.

  1. L.marker([51.5, -0.09], {icon: myIcon}).addTo(this.map);

React: Leaflet Control

This entry is part 7 of 9 in the series React: Leaflet

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);

React: Leaflet LayerGroup

This entry is part 6 of 9 in the series React: Leaflet

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");

React: Leaflet DomEvent

This entry is part 5 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add events to your html control. Refer to the documentation for more information.

Before We Begin:

OnClick

  1. L.DomEvent.on(htmlControl, "click", (e) => {
  2. //Do your work here
  3. });

 

 

 

React: Leaflet html Controls

This entry is part 4 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add html controls to your leaflet map. Refer to the documentation for more information.

Before We Begin:

Really you can create any html control just substitute div for whatever you want. Below are just some basic examples.

Div

  1. var divContainer = L.DomUtil.create("div", "myClassName");

Image

  1. var img = L.DomUtil.create("img", "myClassName");

Label

  1. var label = L.DomUtil.create("label", "myClassName");

Span

  1. var span = L.DomUtil.create("span", "myClassName");

Input

  1. var input = L.DomUtil.create("input", "myClassName");

BR

  1. var br = L.DomUtil.create("br", "myClassName");

You can modify the html control with additional values such as

  1. #style
  2. divContainer.style.display = "";
  3.  
  4. #id
  5. divContainer.id = "myId";
  6.  
  7. #name
  8. divContainer.name = "myId";
  9.  
  10. #img src
  11. divContainer.src = "";
  12.  
  13. #title
  14. divContainer.title = "";
  15.  
  16. #innerHTML
  17. divContainer.innerHTML = "";
  18.  
  19. #type
  20. divContainer.type= "checkbox";
  21.  
  22. #checked
  23. divContainer.checked= false;
  24.  
  25.  

Parent Control

You can add the control to a parent control in the following way.

  1. var divContainer = L.DomUtil.create("div", "myClassName");
  2.  
  3. var subContainer = L.DomUtil.create("div", "myClassName", divContainer);

React: Leaflet EasyButton

This entry is part 3 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a easy button to your leaflet map. This will just be a basic example. For more information refer to the documentation.

Before We Begin:

Node Package Install:

  1. npm install leaflet-easybutton --save

Edit app/home/leaflet/js/map.jsx:

  1. //Add the leaflet easybutton package
  2. require("leaflet-easybutton");
  3. require("leaflet-easybutton/src/easy-button.css");
  4.  
  5. //Somehwere on your page add the following.
  6.  
  7.  
  8. //Check out https://github.com/CliffCloud/Leaflet.EasyButton for more uses
  9. L.easyButton("<div id="tag" class="glyphicon glyphicon-tag" />", function(btn, map) {
  10. map.setView([42.3748204,-71.1161913],16);
  11. }, { position: "topleft"}).addTo(this.map);
  12.  

React: Leaflet Modal

This entry is part 2 of 9 in the series React: Leaflet

In this tutorial I will demonstrate how to add a modal dialog to your leaflet map. Refer to the documentation for more information.

Before We Begin:

Node Package Install:

  1. npm install leaflet-modal --save

Edit app/home/leaflet/js/map.jsx:

  1. //Add the leaflet modal package
  2. require("leaflet-modal");
  3. require("leaflet-modal/dist/leaflet.modal.min.css");
  4.  
  5. //Somewhere on your page add the following. You can even put it on a onclick event or whatever
  6.  
  7. //Create a test control for your modal control.
  8. this.modalContainer = L.DomUtil.create("div");
  9. map.openModal({
  10. zIndex: 10000,
  11. element: this.modalContainer, //The container to display in the modal
  12. OVERLAY_CLS: "overlay", //This is a built in class which you can change if you want
  13. MODAL_CLS: "modal", //This is a built in class which you can change if you want
  14. height: 200, width: 200,
  15. MODAL_CONTENT_CLS: "modal-content", //This is a built in class which you can change if you want
  16. CLOSE_CLS: "modal-close-hide" //The property for close class
  17. });

Edit app/home/leaflet/css/map.css:

  1. .modal-close-hide {
  2. /*Class for hiding the modal*/
  3. display: none;
  4. }

React: Basic Leaflet Map Example

This entry is part 1 of 9 in the series React: Leaflet

In this tutorial I will walk you through incorporating leaflet map in your application. This is just a basic setup walkthrough. We do not deal with overlays or extending controls.

Documentation:

Leaflet
Leaflet Providers

Before We Begin:

If you have not done so already refer to how to Build a React/Python Site to get your basic site up and running.

Node Package Installs:

  1. npm install leaflet-providers --save
  2. npm install leaflet --save

Create Needed Folders/Files:

  • app
    • folder: leaflet
      • folder: css
        • file: map.css
      • folder: js
        • file: map.jsx

Webpack:

Add the following loader for handling images to your webpack.config.js file.

  1. { test: /.*\.(gif|png|jpe?g|svg)$/i, loader: "file-loader?hash=sha512&digest=hex&name=[name].[ext]" }

Edit app/leaflet/js/map.jsx:

We are going to create the map control. Refer to comments below for explanations.

  1. window.jQuery = window.$ = require("jquery");
  2. //React packages
  3. var React = require("react");
  4. var createReactClass = require("create-react-class");
  5.  
  6. //Leaflet packages
  7. import L from "leaflet";
  8. import "leaflet/dist/leaflet.css";
  9.  
  10. //LeafLet providers
  11. require("leaflet-providers");
  12.  
  13. //map css you will create
  14. import "../css/map.css";
  15.  
  16. var LeafLetMap = createReactClass({
  17. getInitialState: function() {
  18. return {};
  19. },
  20. displayMap: function() {
  21. //Setup/initialize the map control
  22. window.map = this.map = L.map("map", {
  23. attributionControl: false, //http://leafletjs.com/reference-1.2.0.html#control-attribution
  24. }).setView([0,0], 1);
  25. //A simple scale control that shows the scale of the current center of screen in metric (m/km) and imperial (mi/ft)
  26. L.control.scale({ //http://leafletjs.com/reference-1.2.0.html#control-scale
  27. metric: false,
  28. imperial: true
  29. }).addTo(this.map);
  30. //You can check out all the free providers here https://github.com/leaflet-extras/leaflet-providers
  31. //Below are just examples
  32. this.layers = {
  33. OpenMapSurferRoads: L.tileLayer.provider('OpenMapSurfer.Roads'),
  34. HyddaFull: L.tileLayer.provider('Hydda.Full'),
  35. StamenTerrain: L.tileLayer.provider('Stamen.Terrain'),
  36. EsriWorldStreetMap: L.tileLayer.provider('Esri.WorldStreetMap'),
  37. EsriWorldTopoMap: L.tileLayer.provider('Esri.WorldTopoMap'),
  38. EsriWorldTerrain: L.tileLayer.provider('Esri.WorldTerrain'),
  39. OpenTopoMap: L.tileLayer.provider('OpenTopoMap'),
  40. OpenStreetMapBlackAndWhite: L.tileLayer.provider('OpenStreetMap.BlackAndWhite'),
  41. OpenStreetMapHOT: L.tileLayer.provider('OpenStreetMap.HOT')
  42. };
  43.  
  44. //Add the default layer you want to use
  45. this.layers.OpenMapSurferRoads.addTo(this.map);
  46.  
  47. //Add the layer control to the top left corner
  48. this.layerControl = L.control.layers(this.layers, {}, { position: "topleft" }).addTo(map);
  49. },
  50. componentDidMount: function() {
  51. //Done after mounting so that it sees the div you are going to map to
  52. this.displayMap();
  53. },
  54. render: function() {
  55. return (
  56. <div className="div-flex">
  57. <div id="map" key="map" className="map"></div>
  58. </div>
  59. );
  60. }
  61. });
  62.  
  63. module.exports = { LeafLetMap:LeafLetMap };

Edit app/leaflet/css/map.css:

  1. .map {
  2. /*Changes the width and height of the map*/
  3. height: 400px;
  4. width: 800px;
  5. }
  6.  
  7. div.leaflet-top {
  8. /*OPTIONAL: This is only needed if your controls start going behind the map.*/
  9. z-index: 1001;
  10. }
  11.  
  12. .leaflet-control-layers-toggle {
  13. /*Changes the width and height of the layer control*/
  14. height: 30px !important;
  15. width: 30px !important;
  16. }

Edit app/home/js/home.jsx:

  1. //Import your leaflet map control from the page you created above
  2. import { LeafLetMap } from "../../leaflet/js/map.jsx";
  3.  
  4. //In the render function add your control
  5. render: function() {
  6. return (
  7. <div className="div-flex">
  8. <LeafLetMap ref="map" />
  9. </div>
  10. );
  11. }