diff --git a/src/components/UserMap.js b/src/components/UserMap.js index c1c4af80347a4d2a1d54b8aa9b2309c4aff5d172..f7add78a53a9049dcd6464400f5c708e270c13aa 100644 --- a/src/components/UserMap.js +++ b/src/components/UserMap.js @@ -8,7 +8,6 @@ import { } from 'react-leaflet' import DrawTools from './DrawTools.js' - class UserMap extends Component { constructor(props){ super(props); @@ -17,30 +16,21 @@ class UserMap extends Component { ownLng: null, } - this.DeviceLocationUpdater = null; - this.updateDeviceLocation = this.updateDeviceLocation.bind(this); + this.watchPositionId = null; } componentDidMount(){ - this.startDeviceLocationUpdater(); + this.getCurrentPosition((position) => { + this.setCurrentPosition(position); + }); } componentWillUnmount(){ - if(this.DeviceLocationUpdater != null){ - clearInterval(this.DeviceLocationUpdater); + if(this.watchPositionId != null){ + navigator.geolocation.clearWatch(this.watchPositionId); } } - startDeviceLocationUpdater(){ - this.DeviceLocationUpdater = setInterval(this.updateDeviceLocation, 1000); - } - - updateDeviceLocation(){ - this.getCurrentPosition((position) => { - this.setCurrentPosition(position); - }); - } - setCurrentPosition(position){ this.setState({ ownLat: position.coords.latitude, @@ -60,7 +50,9 @@ class UserMap extends Component { maximumAge: 0 } - navigator.geolocation.getCurrentPosition((position) =>{ + if(this.watchPositionId != null){navigator.geolocation.clearWatch(this.watchPositionId);} + + this.watchPositionId = navigator.geolocation.watchPosition((position) =>{ //success if(position != null){ callback(position);