如何使用经度和纬度计算两点之间的距离(以英里为单位)?

时间:2019-07-30 02:01:10

标签: php laravel

我在数据库中有许多经度和经度的商店,并且我的用户具有经度和经度,因此我想计算每个商店与用户之间的距离,因此,如果商店的覆盖范围小于用户的距离,请查看此商店,我希望有一个很好的快捷方法来解决此问题 我曾尝试使用haversine,但不知道如何处理。请注意,我是Laravel的新手,谢谢

 public function getaddress(Request $request)
 {
     if(!empty($request->thislat)) {

         $thislat=$request->thislat;
         $thislng=$request->thislng;
         $storeinfo=Storeinfo::all();

         //$storeinfo->store_lat;
        //$storeinfo->store_long;
     }
 }

我想像这样返回两个坐标之间的距离:

     $dist = "( 6371 * acos( cos( radians({$thislat}) ) * cos( radians(41.9625898) ) * cos( radians(-87.65535779999999) - radians({$thislng}) ) + sin( radians({$thislat}) ) * sin( radians(41.9625898) ) ) ) AS distance";

1 个答案:

答案 0 :(得分:0)

请尝试一下。它会从用户那里找到5KM商店。

$radius = 5; //It'll find 5KM stores around users
$thislat= $request->thislat;
$thislng= $request->thislng;

$dist = "( 6371 * acos( cos( radians({$thislat}) ) * cos( radians( `store`.`store_lat` ) ) * cos( radians( `store`.`store_long` ) - radians({$thislng}) ) + sin( radians({$thislat}) ) * sin( radians( `store`.`store_lat` ) ) ) ) AS distance";

$stores = Storeinfo::select("*")->selectRaw($dist)->having("distance", "<=", $radius)->get();

如果要找到两个距离点,则可以使用此功能。

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    if (($lat1 == $lat2) && ($lon1 == $lon2)) {
         return 0;
    } else {
        $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 == "N") {
          return ($miles * 0.8684);
        } else {
          return $miles;
        }
     }
}
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";

function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) {
// Calculate the distance in degrees
$degrees = rad2deg(acos((sin(deg2rad($point1_lat))*sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat))*cos(deg2rad($point2_lat))*cos(deg2rad($point1_long-$point2_long)))));

// Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles)
    switch($unit) {
        case 'km':
            $distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km)
            break;
        case 'mi':
            $distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles)
            break;
        case 'nmi':
            $distance =  $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles)
    }
    return round($distance, $decimals);
}

$point1 = array("lat" => "48.8666667", "long" => "2.3333333"); // Paris (France)
$point2 = array("lat" => "19.4341667", "long" => "-99.1386111"); // Mexico City (Mexico)

$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); // Calculate distance in kilometres (default)

$mi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'mi'); // Calculate distance in miles

$nmi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'nmi'); // Calculate distance in nautical miles

echo "The distance between Paris (France) and Mexico City (Mexico) is $km km (= $mi miles = $nmi nautical miles)";