Haversine formula with php

后端 未结 5 866
不知归路
不知归路 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 05:59

    public function getDistanceBetweenTwoPoints($point1 , $point2){
        // array of lat-long i.e  $point1 = [lat,long]
        $earthRadius = 6371;  // earth radius in km
        $point1Lat = $point1[0];
        $point2Lat =$point2[0];
        $deltaLat = deg2rad($point2Lat - $point1Lat);
        $point1Long =$point1[1];
        $point2Long =$point2[1];
        $deltaLong = deg2rad($point2Long - $point1Long);
        $a = sin($deltaLat/2) * sin($deltaLat/2) + cos(deg2rad($point1Lat)) * cos(deg2rad($point2Lat)) * sin($deltaLong/2) * sin($deltaLong/2);
        $c = 2 * atan2(sqrt($a), sqrt(1-$a));
    
        $distance = $earthRadius * $c;
        return $distance;    // in km
    }
    

提交回复
热议问题