【发布时间】:2019-05-30 14:48:15
【问题描述】:
我的地理位置是 lat + lon。我正在尝试添加 getDistance 和 isPointInCircle 函数,它们都可以工作,直到我想用 myLAT + myLON 替换一个点。
在很好的帮助下,我被推荐声明“const { latitude, longitude } = this.state;”但我错过了一些东西,因为我仍然得到错误。我尝试将函数放在“componentDidMount()”内部和外部,但我无法使其工作。
import geolib from "geolib";
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
speed: null,
error: null,
};
}
componentDidMount() {
this.watchId = navigator.geolocation.watchPosition(
(position) => {
const { latitude, longitude } = this.state;
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
speed: position.coords.speed,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 1},
);
}
componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchId);
}
render() {
return (
<View style={styles.container}>
<View style={{ flexGrow: 0.3, alignItems: 'center', justifyContent: 'center' }}>
<Text>GeoLib:: Distance: {DIST} meters</Text> //I'd like to put the DISTANCE here
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
let RADIUS = geolib.isPointInCircle(
{ latitude: latitude, longitude: longitude },
{latitude: 37.600530, longitude: -122.482629},
1000
);
let DIST = geolib.getDistance(
{ latitude: latitude, longitude: longitude },
{latitude: 37.600530, longitude: -122.482629}
);
if(RADIUS == true){
console.log("I am in Radius.");
}else{
console.log("I am NOT in Radius.");
}
【问题讨论】:
标签: react-native object variables