diff --git a/src/components/UserMap.js b/src/components/UserMap.js index 69ad8d872814a132877e8d1c2cefd167c15bf6b2..2fd70bc708032c147136f144936581a043bae894 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, }); }