From 92e66ce967c23bf97b654999b5c4646d06a4941a Mon Sep 17 00:00:00 2001 From: Joni Laukka <joni.laukka.overflow@gmail.com> Date: Thu, 6 Jun 2019 13:44:44 +0300 Subject: [PATCH] GetCurrentPosition interval changed to WatchPosition so the device does not ask permission every time the location is asked. --- src/components/UserMap.js | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/components/UserMap.js b/src/components/UserMap.js index c1c4af8..f7add78 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); -- GitLab