[英]Calculate distance between GPS points by coordinates
I'm having some trouble computing the distance between two GPS points by their coordinates. 我在通过两个GPS坐标计算它们之间的距离时遇到了麻烦。
point a
x = 7,2562
y = 47,7434599999999
point b
x = 7,21978
y = 47,73836
I used the Haversine formula as described here . 我使用了此处所述的Haversine公式。 The result I get is 4.09 km.
我得到的结果是4.09公里。
However, locating those points on a map using a tool like this , I can measure a distance of 2.8 km 然而,使用类似的工具在地图上定位这些点这个 ,我可以测量距离2.8公里
Several other formulas I tried also return a result around 4 km. 我尝试的其他几个公式也返回了大约4 km的结果。
Any ideas what I would be missing ? 有什么想法我会想念的吗?
I think is because u are using the function in miles, in Kms you can use something like that: 我认为是因为您使用的是以英里为单位的函数,因此您可以在Kms中使用类似的方法:
public static function distance(
array $from,
array $to
) {
if (empty($from['lat']) || empty($to['lat'])) {
return $to['distance'];
}
$latitude1 = (float) $from['lat'];
$latitude2 = (float) $to['lat'];
$longitude1 = (float) $from['lng'];
$longitude2 = (float) $to['lng'];
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)))
+ (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)))
;
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
$distance = (is_nan($distance)) ? 0 : $distance * 1.609344;
return $distance;
}
As pointed out by Roland Starke in the comments, the problem was the order of the coordinates. 正如罗兰·斯塔克(Roland Starke)在评论中指出的那样,问题在于坐标的顺序。 (7, 47 not 47, 7)
(7,47不是47,7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.