问题描述
我有 2 个坐标.坐标 1 是人".坐标 2 是目的地.
I have 2 coordinates. Coordinate 1 is a 'person'. Coordinate 2 is a destination.
如何将坐标 1 100 米移近坐标 2?
How do I move coordinate 1 100 meters closer to coordinate 2?
这将用于 cron 作业,因此仅包括 php 和 mysql.
This would be used in a cron job, so only php and mysql included.
例如:
联系人在:51.26667, 3.45417
Person is at: 51.26667, 3.45417
目的地是:51.575001, 4.83889
Destination is: 51.575001, 4.83889
我将如何计算距离 Person 更近 100 米的新坐标?
How would i calculate the new coordinates for Person to be 100 meters closer?
推荐答案
使用Haversine计算两点之间的差,以米为单位;然后按比例调整人物坐标的值.
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;
培养你的价值观:
$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 公里
gives a result of 102059.82251083 metres, 102.06 kilometers
调整比例为 100/102059.82251083 = 0.0009798174985988102859004569070625
The ratio to adjust by is 100 / 102059.82251083 = 0.0009798174985988102859004569070625
$newLat = $lat + (($lat2 - $lat) * $ratio);
$newLng = $lng + (($lng2 - $lng) * $ratio);
给出新的纬度 51.266972108109 和经度 3.4555267728867
Gives a new latitude of 51.266972108109 and longitude of 3.4555267728867
这篇关于如何使用坐标将标记移动 100 米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!