Haversine formula with php

后端 未结 5 871
不知归路
不知归路 2020-11-28 05:27

I want to use this formula with php. I have a database with some values of latitute and longitude saved.

I want to find, with a certain value of latitude and longit

5条回答
  •  情话喂你
    2020-11-28 06:07

    I making the class of haversign which having the static fuction getDistance having the four paramters and it return the distance from the bot location points

    class HaverSign {
    
         public static function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
            $earth_radius = 6371;
    
            $dLat = deg2rad($latitude2 - $latitude1);
            $dLon = deg2rad($longitude2 - $longitude1);
    
            $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
            $c = 2 * asin(sqrt($a));
            $d = $earth_radius * $c;
    
            return $d;
    }
    }
    

    above class is stored at the root directory and root directory contains classes folder Call this by using the following way in any php page

    include "../classes/HaverSign.php";
    $haversign=new HaverSign();
    
    $lat=18.5204;
    $lon=73.8567;
    
    $lat1=18.5404;
    $lon1=73.8167;
    
    $dist = $haversign->getDistance($lat,$lon,$lat1,$lon1);
    echo $dist;
    

    Output is as follow

    4.7676529976827
    

提交回复
热议问题