Files
deighton-ar/mobile/src/ui/Geolocation.js
2018-05-28 16:12:20 -07:00

44 lines
896 B
JavaScript

import React, { Component } from "react"
import { Image, View } from "react-native"
import PropTypes from "prop-types"
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)
)
}
})
}
componentWillUnmount() {
if (this.watchId !== -1) {
navigator.geolocation.clearWatch(this.watchId)
navigator.geolocation.stopObserving()
this.watchId = -1
}
}
render() {
return null
}
}