【问题标题】:Calculating position from another GPS position + distance从另一个 GPS 位置 + 距离计算位置
【发布时间】:2019-12-16 15:19:45
【问题描述】:

开发一个 Javascript 应用程序,我正在寻找一种方法来计算目标点的 GPS 坐标,该坐标来自另一个位置(纬度|经度)+一定距离(例如再往南 20 公里)。将其表达为一个口头问题,可以说:我当前位置的 GPS 坐标加上北|南 20 公里的 GPS 坐标是多少?

伪公式:

目标位置 = 已知位置 + 距离

在哪里:

  • TargetPosition 由(纬度|经度)组成
  • KnownPosition 由(纬度|经度)组成
  • 距离是进一步向北或向南的直线向量

对公式有什么想法吗?

【问题讨论】:

    标签: javascript math


    【解决方案1】:

    一旦我们知道以公里为单位的经向距离,并且知道这是纬度的 360°,我们就可以计算由于向北或向南移动 x 公里而产生的偏移。

    每向北或向南移动度数,我们移动 (40007.86 / 360) = 111.13 公里。

    我们还将对两极附近的位置进行一些错误检查..

    我将添加一个更通用的公式,用于在给定北部和东部偏移的情况下获取新位置。 (按照通常的惯例,对于南部和西部为负数),这仅适用于小位移。

    function getNewLatitude(latitude, distanceKm) {
        const meridionalRadiuskm = 40007.86;
        latitude = (latitude + distanceKm / (meridionalRadiuskm / 360));
        if (latitude > 90) return 180 - latitude;
        if (latitude < -90) return -(180 + latitude);
        return latitude;
    }
    
    console.log(getNewLatitude(50, 100));
    console.log(getNewLatitude(50, -100));
    
    // This function may also be useful, you can use this to get a new location base on a north/south / east/west offset in km. Note that accuracy will start to reduce as the offset increases. 
    function getNewLocation(lat, lon, offsetNorthKm, offsetEastKm) {
        // The approximate distance in kilometres of one degree at 0,0.
        const ONE_DEGREE_KM = 111.32;
    
        const deltaLatitude = offsetNorthKm / ONE_DEGREE_KM;
        const deltaLongitude = offsetEastKm / (ONE_DEGREE_KM * Math.cos(Math.PI * lat / 180));
    
        let result = { 
            lat:  lat + deltaLatitude,
            lon:  lon + deltaLongitude
        }
        return result;
    }

    【讨论】:

      【解决方案2】:

      您可以使用Geolib 库,它会为您提供更精确的结果,尤其是在两极附近。

      示例:这将从提供​​的纬度/经度计算 15000 米,方位角为 180 度(南)。

      geolib.computeDestinationPoint(
          { latitude: 52.518611, longitude: 13.408056 },
          15000,
          180
      );
      

      请注意,方向以度为单位(0 = 360 = 北,180 = 南等)。使用这个库还可以让您进行其他有趣的计算,例如:

      • 获取 2 点之间的距离
      • 获取点数组的中心
      • 获取指南针方向
      • 查找最近点
      • 还有更多

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        • 1970-01-01
        • 2018-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多