小编Sou*_*ADI的帖子

如何在react native expo中使用纬度和经度计算两点之间的距离?

我实际上正在开发一个小型反应本机应用程序,我需要计算经度和纬度之间的距离。我有我当前位置的经度和纬度,我有目的地的经度和纬度。

我尝试使用 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 }); …
Run Code Online (Sandbox Code Playgroud)

geolocation

2
推荐指数
2
解决办法
3869
查看次数

标签 统计

geolocation ×1