如何使用坐标将标记移动 100 米

发布于 2024-09-06 05:06:07 字数 240 浏览 3 评论 0原文

我有2个坐标。坐标1是一个“人”。坐标2是目的地。

如何将坐标 1 移近 100 米以靠近坐标 2?

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

例如:

此人的位置:51.26667, 3.45417

目的地是:51.575001, 4.83889

我如何计算 Person 的新坐标以接近 100 米?

I have 2 coordinates. Coordinate 1 is a 'person'. Coordinate 2 is a destination.

How do I move coordinate 1 100 meters closer to coordinate 2?

This would be used in a cron job, so only php and mysql included.

For example:

Person is at: 51.26667, 3.45417

Destination is: 51.575001, 4.83889

How would i calculate the new coordinates for Person to be 100 meters closer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你好,陌生人 2024-09-13 05:06:07

使用半正矢计算两点之间的差(以米为单位);然后按比例调整人物坐标的值。

$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 公里

调整的比率为 100 / 102059.82251083 = 0.0009798174985988102859004569070625

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

给出新的纬度 51.2669721081 09 经度 3.4555267728867

Use Haversine to calculate the difference between the two points in metres; then adjust the value of the person coordinates proportionally.

$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;

Feeding your values of:

$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;

gives a result of 102059.82251083 metres, 102.06 kilometers

The ratio to adjust by is 100 / 102059.82251083 = 0.0009798174985988102859004569070625

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

Gives a new latitude of 51.266972108109 and longitude of 3.4555267728867

微凉 2024-09-13 05:06:07

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

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

现在使用 theta 和您想要向前推进点的量来计算新点。

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

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

Find the angle theta between the x-axis and the vector from person to destination.

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

Now use theta and the amount you want to advance the point to calculate the new point.

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

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

囍孤女 2024-09-13 05:06:07

如果您了解 JavaScript,您可能需要查看以下 Stack Overflow 帖子中的 moveTowards() 方法:

当给定起点、终点以及沿该线行进的距离时,方法返回目的地点。您可以使用点 1 作为起点,点 2 作为终点,距离为 100 米。它是用 JavaScript 编写的,但我确信它可以轻松移植到 PHP 或 MySQL。

您可能还想查看另一篇 Stack Overflow 帖子,它实现了上述 JavaScript 实现的一部分,作为 SQL Server 2008 的用户定义函数,称为 func_MoveTowardsPoint

/stackoverflow.com/questions/2094268/moving-a-point-along-a-path-in-sql-server-2008/2124298#2124298 "> 使用 SQL Server 2008 的内置地理数据类型。不过,您可以轻松地使用两种decimal 数据类型来表示纬度和经度,而不是单个geography 数据类型。

SQL Server 和 JavaScript 示例均基于 Chris Veness 文章 计算距离 中的实现、纬度/经度点之间的方位角等

If you understand JavaScript, you may want to check out the moveTowards() method in the following Stack Overflow post:

This method returns the destination point when given a start point, an end point, and the distance to travel along that line. You can use point 1 as the starting point, point 2 as the end point, and a distance of 100 meters. It's written in JavaScript, but I'm sure it can be easily ported to PHP or MySQL.

You may also want to check out this other Stack Overflow post which implements a part of the above JavaScript implementation, as a user defined function for SQL Server 2008, called func_MoveTowardsPoint:

The above uses SQL Server 2008's in-built geography data type. However you can easily use two decimal data types for latitude and longitude in place of the single geography data type.

Both the SQL Server and the JavaScript examples were based on implementations from Chris Veness's article Calculate distance, bearing and more between Latitude/Longitude points.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文