如何在laravel中实现距离计算?

时间:2017-06-15 12:51:52

标签: php laravel laravel-5

我是laravel的初学者。我不知道如何在laravel框架中实现距离计算。

我的代码:

 function distanceCalculation($point1_lat, $point1_long, $point2_lat,$point2_long, $unit = 'km', $decimals = 2) 
 {
     $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)))));
     switch($unit) 
     {
         case 'km': 
             $distance = $degrees * 111.13384;
             break;
         case 'mi':
             $distance = $degrees * 69.05482;
             break;
         case 'nmi':
             $distance =  $degrees * 59.97662;
     }
     return round($distance, $decimals);
}

以及下面给出的纬度和经度变量。

$point1 = array("lat" => "11.000243", "long" => "76.972726"); 

$point2 = array("lat" => "9.282275", "long" => "79.235395"); 

$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); 

$mi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'mi'); 

$nmi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'nmi'); 

我们在核心php中运行良好,但我想在laravel 5中实现此代码。

2 个答案:

答案 0 :(得分:1)

您可以实现自己的包含$ latitude和$ longitude属性的point类,并将您的逻辑作为距离计算方法,如:

class GeoPoint {

    public $lat;
    public $long;

    public __constuct($latitude, $longitude) {
        $this->lat = $latitude;
        $this->long = $longitude;
    }

    public distanceTo($point, $unit = 'km', $decimals = 2) {
        //your code
        $degrees = rad2deg(acos((sin(deg2rad($this->lat))*sin(deg2rad($point->lat))) + (cos(deg2rad($this->lat))*cos(deg2rad($point->lat))*cos(deg2rad($this->long-$point->long)))));


        switch($unit) 
        {
            case 'km': 
               $distance = $degrees * 111.13384;
               break;
            case 'mi':
               $distance = $degrees * 69.05482;
               break;
            case 'nmi':
               $distance =  $degrees * 59.97662;
        }

        return round($distance, $decimals);
    }
}

然后你可以使用它(在模型或控制器中),如

$point1 = new GeoPoint(11.000243, 76.972726);
$point2 = new GeoPoint(9.282275, 79.235395);

$km = $point1->distanceTo($point2);
$mi = $point1->distanceTo($point2, 'mi');
$nmi = $point1->distanceTo($point2, 'nmi');

如果你使用MySQL数据库来获取坐标,那么有一个更方便的解决方案:https://www.codetutorial.io/geo-spatial-mysql-laravel-5/ 在最好的情况下,您可以将它放入一个抽象类(从Eloquent派生),以便以后重用代码。

答案 1 :(得分:1)

将此文件保存在/app/Helpers/CommonHelper.php中        

    namespace App\Helpers;
    class CommonHelper
    {
         public static function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) 
         {

               $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)))));


              switch($unit) 
              {
                  case 'km': 
                       $distance = $degrees * 111.13384;
                       break;
                  case 'mi':
                       $distance = $degrees * 69.05482;
                       break;
                  case 'nmi':
                       $distance =  $degrees * 59.97662;
              }

              return round($distance, $decimals);
        }

    }

您可以通过导入名称空间use App\Helpers\CommonHelper;来访问它 CommonHelper::distanceCalculation()

我希望你能得到它。