如何使用坐标将标记移动 100 米
我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用半正矢计算两点之间的差(以米为单位);然后按比例调整人物坐标的值。
输入您的值:
给出的结果为 102059.82251083 米,102.06 公里
调整的比率为 100 / 102059.82251083 = 0.0009798174985988102859004569070625
给出新的纬度 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.
Feeding your values of:
gives a result of 102059.82251083 metres, 102.06 kilometers
The ratio to adjust by is 100 / 102059.82251083 = 0.0009798174985988102859004569070625
Gives a new latitude of 51.266972108109 and longitude of 3.4555267728867
求 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
如果您了解 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 twodecimal
data types for latitude and longitude in place of the singlegeography
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.