Calculating distance between zip codes in PHP

前端 未结 5 1838
北荒
北荒 2020-11-28 04:32

I grabbed a database of the zip codes and their langitudes/latitudes, etc from this This page. It has got the following fields:

ZIP, LATITUDE, LONGIT

5条回答
  •  日久生厌
    2020-11-28 04:56

    It can be done with just maths...

    function calc_distance($point1, $point2)
    {
        $distance = (3958 * 3.1415926 * sqrt(
                ($point1['lat'] - $point2['lat'])
                * ($point1['lat'] - $point2['lat'])
                + cos($point1['lat'] / 57.29578)
                * cos($point2['lat'] / 57.29578)
                * ($point1['long'] - $point2['long'])
                * ($point1['long'] - $point2['long'])
            ) / 180);
    
        return $distance;
    }
    

提交回复
热议问题