From 22a6f91035b627ba287aeff5890ad12eb333ceaa Mon Sep 17 00:00:00 2001
From: Joni Laukka <joni.laukka.overflow@gmail.com>
Date: Thu, 6 Jun 2019 10:58:52 +0300
Subject: [PATCH] Device location update interval added. Location will be
 updated every second.

---
 src/components/UserMap.js | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/components/UserMap.js b/src/components/UserMap.js
index 69ad8d8..2fd70bc 100644
--- a/src/components/UserMap.js
+++ b/src/components/UserMap.js
@@ -8,14 +8,35 @@ class UserMap extends Component {
       ownLat: null,
       ownLng: null,
     }
+
+    this.DeviceLocationUpdater = null;
+    this.updateDeviceLocation = this.updateDeviceLocation.bind(this);
   }
 
   componentDidMount(){
+    this.startDeviceLocationUpdater();
+  }
+
+  componentWillUnmount(){
+    if(this.DeviceLocationUpdater != null){
+      clearInterval(this.DeviceLocationUpdater);
+    }
+  }
+
+  startDeviceLocationUpdater(){
+    this.DeviceLocationUpdater = setInterval(this.updateDeviceLocation, 1000);
+  }
+
+  updateDeviceLocation(){
     this.getCurrentPosition((position) => {
-      this.setState({
-        ownLat: position.coords.latitude,
-        ownLng: position.coords.longitude,
-      });
+      this.setCurrentPosition(position);
+    });
+  }
+
+  setCurrentPosition(position){
+    this.setState({
+      ownLat: position.coords.latitude,
+      ownLng: position.coords.longitude,
     });
   }
 
-- 
GitLab