Browse Source

leaflet draw: first try example

Bernadette Elena Hammerle 3 years ago
parent
commit
51e791d1de
4 changed files with 72 additions and 3 deletions
  1. 18 0
      package-lock.json
  2. 2 0
      package.json
  3. 1 0
      public/index.html
  4. 51 3
      src/maps/Leaflet.js

+ 18 - 0
package-lock.json

@@ -12670,6 +12670,11 @@
       "resolved": "https://registry.npmjs.org/leaflet/-/leaflet-1.7.1.tgz",
       "integrity": "sha512-/xwPEBidtg69Q3HlqPdU3DnrXQOvQU/CCHA1tcDQVzOwm91YMYaILjNp7L4Eaw5Z4sOYdbBz6koWyibppd8Zqw=="
     },
+    "leaflet-draw": {
+      "version": "1.0.4",
+      "resolved": "https://registry.npmjs.org/leaflet-draw/-/leaflet-draw-1.0.4.tgz",
+      "integrity": "sha512-rsQ6saQO5ST5Aj6XRFylr5zvarWgzWnrg46zQ1MEOEIHsppdC/8hnN8qMoFvACsPvTioAuysya/TVtog15tyAQ=="
+    },
     "left-pad": {
       "version": "1.3.0",
       "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz",
@@ -12811,6 +12816,11 @@
       "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
       "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
     },
+    "lodash-es": {
+      "version": "4.17.15",
+      "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.15.tgz",
+      "integrity": "sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ=="
+    },
     "lodash._reinterpolate": {
       "version": "3.0.0",
       "resolved": "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz",
@@ -16223,6 +16233,14 @@
         "warning": "^4.0.3"
       }
     },
+    "react-leaflet-draw": {
+      "version": "0.19.0",
+      "resolved": "https://registry.npmjs.org/react-leaflet-draw/-/react-leaflet-draw-0.19.0.tgz",
+      "integrity": "sha512-aOB7Nqgl79l62L7vHxhdyKJD6ep+1Q+qTfnrYfmcgF+yK0A1lQA2fUv9N4C0HCbejcyiqx1XYchSCw9Q+Vtc3A==",
+      "requires": {
+        "lodash-es": "^4.17.10"
+      }
+    },
     "react-lifecycles-compat": {
       "version": "3.0.4",
       "resolved": "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz",

+ 2 - 0
package.json

@@ -14,6 +14,7 @@
     "google-map-react": "^2.1.8",
     "jquery": "^3.5.1",
     "leaflet": "^1.7.1",
+    "leaflet-draw": "^1.0.4",
     "mapbox-gl": "^1.12.0",
     "ol": "^6.0.1",
     "pigeon-draggable": "^0.1.1",
@@ -23,6 +24,7 @@
     "react": "^16.14.0",
     "react-dom": "^16.14.0",
     "react-leaflet": "^2.7.0",
+    "react-leaflet-draw": "^0.19.0",
     "react-map-gl": "^5.2.9",
     "react-map-gl-draw": "^0.21.1",
     "react-mapbox-gl": "^4.8.6",

+ 1 - 0
public/index.html

@@ -4,6 +4,7 @@
     <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
       integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
       crossorigin=""/>
+    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css"/>
     <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
       integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
       crossorigin=""></script>

+ 51 - 3
src/maps/Leaflet.js

@@ -5,8 +5,11 @@ import {
   Marker,
   Popup,
   ZoomControl,
-  Polyline
+  Polyline,
+  FeatureGroup
 } from "react-leaflet";
+import {EditControl} from "react-leaflet-draw";
+import L from "leaflet";
 import "./../css/maps.css";
 import {calcDistance} from "./../helpers/distance";
 import {longitude, latitude, mapWidth, mapHeight} from "./../config/variables";
@@ -55,6 +58,42 @@ export default class Leaflet extends React.Component{
     this.setState({path: [], distance: 0})
   }
 
+
+  _onEdited = (e) => {
+    let numEdited = 0;
+    e.layers.eachLayer( (layer) => {
+      numEdited += 1;
+    });
+    this._onChange();
+  }
+
+  _onCreated = (e) => {
+    let type = e.layerType;
+    let layer = e.layer;
+    // Do whatever else you need to. (save to db; etc)
+    this._onChange();
+  }
+
+  _onDeleted = (e) => {
+    let numDeleted = 0;
+    e.layers.eachLayer( (layer) => {
+      numDeleted += 1;
+    });
+    this._onChange();
+  }
+
+  _editableFG = null
+
+  _onChange = () => {
+    // this._editableFG contains the edited geometry, which can be manipulated through the leaflet API
+    const { onChange } = this.props;
+    if (!this._editableFG || !onChange) {
+      return;
+    }
+    const geojsonData = this._editableFG.leafletElement.toGeoJSON();
+    onChange(geojsonData);
+  }
+
   render() {
     const position = [this.state.latitude, this.state.longitude]
     let testMarkers = this.props.markers
@@ -92,12 +131,21 @@ export default class Leaflet extends React.Component{
                 attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                 url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
               />
-              <ZoomControl position="bottomright" />
 
-              <Polyline color="grey" positions={this.state.path} />
+              <FeatureGroup>
+                <EditControl
+                  position='topright'
+                  onEdited={this._onEdited}
+                  onCreated={this._onCreated}
+                  onDeleted={this._onDeleted}
+                  draw={{rectangle: false}}
+                />
+              </FeatureGroup>
 
               {markers}
+              <Polyline color="grey" positions={this.state.path} />
 
+            <ZoomControl position="bottomright" />
           </Map>
         </div>