React: Basic Leaflet Map Example

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

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. }

 

 

 

Series NavigationReact: Leaflet Modal >>