From 31817ed3b58d5c54e1e13d3407caae79604bc5b6 Mon Sep 17 00:00:00 2001 From: Joni Laukka <joni.laukka.overflow@gmail.com> Date: Wed, 5 Jun 2019 09:06:00 +0300 Subject: [PATCH] Geolocation options added to get more accurate location --- src/components/UserMap.js | 40 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/components/UserMap.js b/src/components/UserMap.js index 1a28393..69ad8d8 100644 --- a/src/components/UserMap.js +++ b/src/components/UserMap.js @@ -12,27 +12,33 @@ class UserMap extends Component { componentDidMount(){ this.getCurrentPosition((position) => { - if(position != null){ - this.setState({ - ownLat: position.lat, - ownLng: position.lng, - }); - } + this.setState({ + ownLat: position.coords.latitude, + ownLng: position.coords.longitude, + }); }); } getCurrentPosition(callback){ if(!navigator.geolocation){ console.log("Can't get geolocation :/"); - callback(null); } else{ - navigator.geolocation.getCurrentPosition((position)=> { - callback({ - lat: position.coords.latitude, - lng: position.coords.longitude - }); - }); + // Position tracking options + const options = { + enableHighAccuracy: true, + timeout: 30000, + maximumAge: 0 + } + + navigator.geolocation.getCurrentPosition((position) =>{ + //success + if(position != null){ + callback(position); + } + }, (error) =>{ + console.log(error); + }, options); } } @@ -48,10 +54,14 @@ class UserMap extends Component { Se on perjantai, my dudes <br /> </Popup> </Marker> - {this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}></Marker>} + {this.state.ownLat !== null && <Marker position={[this.state.ownLat, this.state.ownLng]}> + <Popup> + User's real position.<br /> + </Popup> + </Marker>} </Map> ); } } -export default UserMap; +export default UserMap; \ No newline at end of file -- GitLab