Google 地图 - 点与折线的距离

Google Maps - point distance from polyline

我有一些 google 地图折线。 我试图在它们周围画 2 条多段线,所以它们形成了某种边界:

因此,对于原始多段线的每个点,我都会计算边界线点,所以可以说距原始点 25 米:

function getOffsetPoints($p1, $p2, $p3, $distance = 25) {
    $gap = (180 / M_PI)  * ($distance / 6378137);
    $sx = $p3['lat'] - $p1['lat'];
    $sy = $p3['lng'] - $p1['lng'];
    $cx = $p2['lat']; $cy = $p2['lng'];

    $normx = $sy; $normy = -$sx;
    $length = sqrt($normx * $normx + $normy * $normy);
    $normx = $normx / $length; $normy = $normy / $length;
    
    $newx1 = $cx - $normx * $gap;
    $newy1 = $cy - $normy * $gap;
    $newx2 = $cx + $normx * $gap;
    $newy2 = $cy + $normy * $gap;

    return [
        ['lat'=>$newx1, 'lng'=>$newy1], 
        ['lat'=>$newx2, 'lng'=>$newy2]
    ];
}

对于这个例子,让我们省略多段线的第一个点和最后一个点。所以我总是看上一个点和下一个点,计算中心点的偏移量。

我认为它工作得很好。当我绘制出我的原始点和新计算的点时,它们与原始点的距离相等:

黑色的是原始点,蓝色和红色的是偏移点。

现在我获取所有数据并使用折线将其绘制在 google 地图中:

for (var i=0; i<offset_polylines.length; i++) {
    var oPath = offset_polylines[i];
    var polyline = new google.maps.Polyline({
        path: offset_polylines,
        strokeColor: "#ffffff",
        strokeOpacity: 1,
        strokeWeight: 1,
        map: map,
    });
}

在结果中您可以看到,这些线显然与原始黄线的距离不同。这里发生了什么事?线越 'vertical' 距离越小。这是为什么?如果我在地图上测量距离,我在图片上的 'horizontal line' 和 'vertical' 之间的距离是 10 米。

请注意,比率 meters/degree 对于纬度(N/S 方向,沿子午线)是恒定的,但对于经度方向会根据系数 Abs(Cos(Latitude)) 的纬度发生变化,因此沿 1 度平行于赤道包含 111 公里,但对于北纬 45 度 - 只有 78 公里。

您的计算是粗略的近似值,因此您可以将经度差异 ($sx) 乘以系数 1/Abs(Cos($lat)) 以减少各向异性。

更精确的方法可能包括按描述的距离和方位角计算坐标 here。不要忘记度数和弧度。

dlng = lng3 - lng1

bearing = atan2( sin(dlng)⋅cos(lat3), cos(lat1)⋅sin(lat3) − sin(lat1)⋅cos(lat3)⋅cos(dlng))

perpbearing1 =  bearing + Pi/2
perpbearing2 =  bearing - Pi/2

latperp = asin(sin(lat2)⋅cos(d/R) + cos(lat2)⋅sin(d/R)⋅ cos(perbearingX))
lonperp = lon2+atan2(sin(perpbearingX)⋅sin(d/R)⋅cos(lat2),cos(d/R)−sin(lat2)⋅sin(latperp))