Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用经度和纬度计算距离的结果不同_Php_Mysql_Google Maps_Gps_Latitude Longitude - Fatal编程技术网

Php 使用经度和纬度计算距离的结果不同

Php 使用经度和纬度计算距离的结果不同,php,mysql,google-maps,gps,latitude-longitude,Php,Mysql,Google Maps,Gps,Latitude Longitude,我试图得到两个位置的距离,我真的需要用mySQL来完成,因为我必须过滤很多记录。不幸的是,mysql语句结果与GoogleMaps结果不同。原因可能是什么 我的Mysql语句是(值硬编码) 距离是74.27米 然后我使用了另一个我发现的SQL语句,它给出了85.53米 SELECT (1.609344 * 1000 * 3959 * acos( cos( radians(6.914556) ) * cos( radians( 6.913794 ) ) * cos( radians( 79.973

我试图得到两个位置的距离,我真的需要用mySQL来完成,因为我必须过滤很多记录。不幸的是,mysql语句结果与GoogleMaps结果不同。原因可能是什么

我的Mysql语句是(值硬编码)

距离是74.27米

然后我使用了另一个我发现的SQL语句,它给出了85.53米

SELECT (1.609344 * 1000 * 3959 * acos( cos( radians(6.914556) ) * cos( radians( 6.913794 ) ) * cos( radians( 79.97330 ) - radians(79.973194) ) + sin( radians(6.914556) ) * sin( radians( 6.913794) ) ) ) AS distance 
但是如果我使用谷歌API

我的距离是28米

不管怎样,我都可以解决这个问题。我需要一个在MySQL端工作的解决方案。感谢你的支持

编辑:

我试过用PHP,但我仍然有距离差

<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344);
  } 
  else if ($unit == "M") {
     return ($miles * 1.609344 * 1000);
  }else if ($unit == "N") {
    return ($miles * 0.8684);
  } else {
    return $miles;
  }
}

 function getDrivingDistance($inLatitude,$inLongitude,$outLatitude,$outLongitude)
    {
        if(empty($inLatitude) || empty($inLongitude) ||empty($outLatitude) ||empty($outLongitude))
            return 0;

        // Generate URL
        $url = "http://maps.googleapis.com/maps/api/directions/json?origin=$inLatitude,$inLongitude&destination=$outLatitude,$outLongitude&sensor=false";

        // Retrieve the URL contents
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $url);
        $jsonResponse = curl_exec($c);
        curl_close($c);

        $dataset = json_decode($jsonResponse);
        if(!$dataset)
            return 0;
        if(!isset($dataset->routes[0]->legs[0]->distance->value))
            return 0;
        $distance = $dataset->routes[0]->legs[0]->distance->value;

        return $distance;
    }
echo distance(6.914556,79.973194,6.913794,79.97330,'M') . "<br>";
echo getDrivingDistance(6.914556,79.973194,6.913794,79.97330);

?>

您提供的地图链接具有不同的纬度和经度

        "northeast" : {
           "lat" : 6.913845999999999,
           "lng" : 79.9733002
        },
        "southwest" : {
           "lat" : 6.913794800000001,
           "lng" : 79.9730488
        }

通过在查询中使用它,它给出了正确的距离。

我尝试了大圆公式,结果是85.5m(你的公式不太正确,但很接近)。与谷歌不同的是,谷歌试图计算一条道路上两点之间的距离,但不知何故,它被投射到了道路上的一个点上。见下图。(红线是从坐标绘制的,距离为85.5米,蓝线由谷歌渲染,不知何故被“捕捉”到道路上)

可能是您使用的计算公式不同(且精度较低)——Vincenty和Haversine是两个主要的大圆公式(Vincenty更精确,但计算时需要更多的处理能力),我想谷歌会使用其中一个。。。你使用的是一个更简单的公式(余弦球面定律),它的精确度比前面提到的大圆距离公式要低,是不是因为它更直观,更容易理解?你为什么这么刻薄?
        "northeast" : {
           "lat" : 6.913845999999999,
           "lng" : 79.9733002
        },
        "southwest" : {
           "lat" : 6.913794800000001,
           "lng" : 79.9730488
        }