Browse Source

added python backend and fetch path from it

Bernadette Elena Hammerle 4 years ago
parent
commit
3511e77807
3 changed files with 43 additions and 0 deletions
  1. 16 0
      src/Leaflet.js
  2. 16 0
      src/databaseInterface.py
  3. 11 0
      src/path.json

+ 16 - 0
src/Leaflet.js

@@ -20,6 +20,21 @@ export default class Leaflet extends React.Component{
     distance: 0
     }
 
+  fetchPath = () => {
+    fetch("http://localhost:5000/get-path")
+      .then(res => {
+        if(res.status >= 400) {
+            throw new Error("Server responds with error!");
+        }
+        return res.json();
+      }).then(data => {
+        this.setState({path: data.path})
+        this.getDistance()
+      }, err => {
+        console.log(err)
+      });
+  }
+
   newPathPoint = event => {
     const { lat, lng } = event.latlng
     console.log("Clicked at", lat, "/", lng)
@@ -85,6 +100,7 @@ export default class Leaflet extends React.Component{
         <div className="mapInfo">
           <button onClick={this.clearPath}>Clear path</button>
           <button onClick={this.getDistance}>Calculate Distance</button>
+          <button onClick={this.fetchPath}>fetch path</button>
           <p>
             Distanz: {Math.round(this.state.distance*100)/100} km
           </p>

+ 16 - 0
src/databaseInterface.py

@@ -0,0 +1,16 @@
+from flask import Flask, request
+from flask_cors import CORS
+import json
+
+app = Flask(__name__)
+CORS(app)
+
+@app.route("/get-path", methods=["GET"])
+def get_path():
+    with open("path.json") as pathFile:
+        pathStr = pathFile.read()
+        path = json.loads(pathStr)
+    return path, 200
+
+if __name__=="__main__":
+    app.run(host='0.0.0.0')

+ 11 - 0
src/path.json

@@ -0,0 +1,11 @@
+{
+  "path": [
+    [48.336950, 14.320570],
+    [48.328529, 14.322428],
+    [48.295742, 14.286967],
+    [48.33700859582575, 14.32284576818347],
+    [48.33498697606996, 14.320232290774586],
+    [48.33531639601211, 14.31291472166777]
+  ]
+
+}