51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import React, { Component } from "react"
|
|
import PropTypes from "prop-types"
|
|
import { config } from "../config"
|
|
import { reactAutoBind } from "auto-bind2"
|
|
|
|
export class Geolocation extends Component {
|
|
static propTypes = {
|
|
onUpdate: PropTypes.func.isRequired,
|
|
watch: PropTypes.bool,
|
|
}
|
|
|
|
static defaultProps = {
|
|
watch: true,
|
|
}
|
|
|
|
constructor(props) {
|
|
super(props)
|
|
this.watchId = -1
|
|
}
|
|
|
|
componentDidMount() {
|
|
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
|
|
}
|
|
}
|