Php 从两个坐标计算km差
我得计算一下他们之间的距离Php 从两个坐标计算km差,php,coordinates,distance,Php,Coordinates,Distance,我得计算一下他们之间的距离 (40.851774999999996,14.268123999999998) 并将每个坐标转换为sql查询的结果: $results = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $key => $value) { echo "distance = ". calculateDistance("40.851774999999996","14.268123999999998",$
(40.851774999999996,14.268123999999998)
并将每个坐标转换为sql查询的结果:
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $key => $value) {
echo "distance = ". calculateDistance("40.851774999999996","14.268123999999998",$value['lat'],$value['lng'])."<br>";
}
它给了我一个奇怪的结果,比如:
distance = NAN //-> NAN???
distance = 3392.8405117312 // TOO MUCH!
distance = 3392.8405117312 // TOO MUCH!
...
问题在哪里?有人能帮我修一下吗?:) 在
sin
函数中使用之前,需要将度转换为弧度
$radians = $degrees * (M_PI/180);
再看看函数。它看起来有点不同。在
sin
函数中使用它之前,需要将度转换为弧度
$radians = $degrees * (M_PI/180);
再看看函数。看起来有点不同。根据这个答案:
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
你写道:
$a = sin($dLat/2)*sin($dLat/2) + sin($dLng/2)*sin($dLng/2);
代码中缺少余弦。根据此答案:
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
你写道:
$a = sin($dLat/2)*sin($dLat/2) + sin($dLng/2)*sin($dLng/2);
代码中缺少余弦。您指的是哈弗公式: 这些页面中有大量示例和代码片段:
您指的是哈弗森公式: 这些页面中有大量示例和代码片段:
您能提供最简单的例子吗?太多让人分心的编码你能提供一个最小的例子吗?太多让人分心的代码