我正在制作一个小型的react native应用程序,需要计算经度和纬度之间的距离。我已经拥有当前位置的经度和纬度以及目标地点的经度和纬度。我尝试使用geolib,但是在控制台上始终出现错误:“获取距离时出错'[TypeError: undefined is not an object (evaluating' P.default.getDistance ')]”。以上是导致错误的组件。
_getLocationAsync = async () => {
let location = await Location.getCurrentPositionAsync({});
const region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude
}
this.setState({ location, region});
console.log(this.state.region)
/* it displays me on console my current position like this :
"Object {
"latitude": 36.8386878,
"longitude": 10.2405357,
}" */
this._getDistanceAsync();
}
};
_getDistanceAsync = async () => {
try {
const distance = geolib.getDistance({ latitude: 51.5103, longitude:
7.49347 }, this.state.region)
this.setState({ distance });
console.log(this.state.distance)
} catch (error) {
console.log("error in get distance", error)
}
};
一旦我放置了_getDistanceAsync函数,就会出现错误。非常感谢您的任何建议。