我有两个问题。一个是如果我使用命令 react native run-android 启动应用程序,我可以看到我当前位置的经度和纬度。如果我重新加载屏幕,我会收到一个超时错误,提示“位置请求超时”并且我看不到经度和纬度。下面是图片
我遇到的另一个问题是,当我尝试计算经度和纬度两个坐标之间的距离时,我得到路径错误。以下是我的代码和错误的屏幕截图:
import React, { Component } from 'react';
import { View, Text, TouchableOpacity, Linking } from 'react-native';
import geolib from "geolib";
class GeolocationExample extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
error: null,
};
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
}
render() {
let geolib = require('geo-lib');
let result =geolib.Distance({
p1: { lat: this.state.latitude, lon: this.state.longitude },
p2: { lat: 33.613355, lon: -117.373261 }
});
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
<Text>Distance between two points:result </Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
export default GeolocationExample;
我不确定为什么会出现这个错误。
任何帮助将不胜感激。