Haversine formula with php

后端 未结 5 869
不知归路
不知归路 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:55

    from this link:

    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;
    }
    

    As you can see there are many differences between this as your code. I don't know if you have either a different approach to the formula or maybe some step when converting to PHP went wrong, but the above formula should work.

提交回复
热议问题