geoLocationOptions = {
enableHighAccuracy: false,
maximumAge: 0,
timeout: 5000
}
constructor(props) {
super(props);
//this.currentLog = this.currentLog.bind(this);
this.checkForGeolocation = this.checkForGeolocation.bind(this)
this.seekGeolocationPermission = this.seekGeolocationPermission.bind(this)
}
checkForGeolocation () {
if (navigator.geolocation) {
this.setState({browserSupported: true})
console.log('Your Browser is Supported!');
} else {
this.setState({browserSupported: false})
alert("Your browser is not supported!")
}
return this.state.browserSupported
}
seekGeolocationPermission() {
if (this.checkForGeolocation()) {
navigator.geolocation.getCurrentPosition(function (position) {
console.log('Latitude: ' + position.coords.latitude)
console.log('Longitude: ' + position.coords.longitude)
console.log('Accuracy: ' + position.coords.accuracy)
}, function (error) {
alert('There\'s an error: ' + error.message)
console.log('The error: '+error.code)
}, this.geoLocationOptions)
}
}
componentDidMount () {
this.seekGeolocationPermission()
}
render() {
return (
<div className="permissions_app">
<p>{this.state.browserSupported}</p>
</div>
);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
这就是我写的。应该执行seekGeolocationPermission的第二部分吗?怎么了?
我应该使用componentDidMount()还是update()还是只在render()内部运行它。