Revert to user location from map with better GPS accuracy

This commit is contained in:
John Lyon-Smith
2018-06-28 08:32:00 -07:00
parent d9fe36cdde
commit ea15132d59
6 changed files with 34 additions and 54 deletions

View File

@@ -15,36 +15,35 @@ export class Geolocation extends Component {
constructor(props) {
super(props)
reactAutoBind(this)
if (props.watch) {
this.intervalId = setInterval(this.updateLocation, config.geoSampleDelay)
} else {
this.intervalId = -1
}
this.watchId = -1
}
componentDidMount() {
this.updateLocation()
}
componentWillUnmount() {
if (this.intervalId !== -1) {
clearInterval(this.intervalId)
this.intervalId = -1
}
}
updateLocation() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.props.onUpdate(position)
if (this.props.watch) {
;(this.watchId = navigator.geolocation.watchPosition((position) =>
this.props.onUpdate(position)
)),
null,
{ distanceFilter: 5, maximumAge: 0, enableHighAccuracy: true }
}
},
null,
{ distanceFilter: 5, maximumAge: 0, enableHighAccuracy: true }
)
}
componentWillUnmount() {
if (this.watchId !== -1) {
navigator.geolocation.clearWatch(this.watchId)
navigator.geolocation.stopObserving()
this.watchId = -1
}
}
render() {
return null
}