代码之家  ›  专栏  ›  技术社区  ›  Rene Pot

如何用坐标移动标记100米

  •  5
  • Rene Pot  · 技术社区  · 15 年前

    这将在cron作业中使用,因此只包括php和mysql。

    例如:

    电话:51.26667,3.45417

    目的地:51.575001,4.83889

    我该如何计算新的坐标,让人离我们近100米?

    3 回复  |  直到 5 年前
        1
  •  11
  •   Mark Baker    15 年前

    $radius = 6378100; // radius of earth in meters
    $latDist = $lat - $lat2;
    $lngDist = $lng - $lng2;
    $latDistRad = deg2rad($latDist);
    $lngDistRad = deg2rad($lngDist);
    $sinLatD = sin($latDistRad);
    $sinLngD = sin($lngDistRad);
    $cosLat1 = cos(deg2rad($lat));
    $cosLat2 = cos(deg2rad($lat2));
    $a = ($sinLatD/2)*($sinLatD/2) + $cosLat1*$cosLat2*($sinLngD/2)*($sinLngD/2);
    if($a<0) $a = -1*$a;
    $c = 2*atan2(sqrt($a), sqrt(1-$a));
    $distance = $radius*$c;
    

    满足您的价值观:

    $lat = 51.26667;        //  Just South of Aardenburg in Belgium
    $lng = 3.45417;
    $lat2 = 51.575001;      //  To the East of Breda in Holland
    $lng2 = 4.83889;
    

    计算结果为102059.82251083米,102.06公里

    $newLat = $lat + (($lat2 - $lat) * $ratio);
    $newLng = $lng + (($lng2 - $lng) * $ratio);
    

    给出了新的纬度51.266972108109和经度3.4555267728867

        2
  •  4
  •   John Wang    15 年前

    求x轴和从人到目的地的向量之间的夹角θ。

    theta = Atan2(dest.y-person.y, dest.x-person.x).

    现在用θ和你想推进点的量来计算新的点。

    newPoint.x = advanceDistance * cos(theta) + person.x

    newPoint.y = advanceDistance * sin(theta) + person.y

        3
  •  4
  •   Community CDub    8 年前

    moveTowards()

    当给定起点、终点和沿该线行驶的距离时,此方法返回目标点。可以使用点1作为起点,点2作为终点,距离为100米。它是用JavaScript编写的,但我相信它可以很容易地移植到PHP或MySQL。

    您可能还想查看另一个Stack Overflow post,它实现了上述JavaScript实现的一部分,作为sqlserver2008的用户定义函数,名为 func_MoveTowardsPoint

    上面使用的是SQLServer2008内置的 geography 数据类型。不过,你可以很容易地使用两个 decimal 数据类型。

    sqlserver和JavaScript示例都基于chrisveness文章中的实现 Calculate distance, bearing and more between Latitude/Longitude points .