Calculating distance between zip codes in PHP

前端 未结 5 1817
北荒
北荒 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;
    }
    
    0 讨论(0)
  • 2020-11-28 05:01

    This is mike's answer with some annotations for the magic numbers. It seemed to work fine for me for some test data:

    function calc_distance($point1, $point2)
    {
        $radius      = 3958;      // Earth's radius (miles)
        $deg_per_rad = 57.29578;  // Number of degrees/radian (for conversion)
    
        $distance = ($radius * pi() * sqrt(
                    ($point1['lat'] - $point2['lat'])
                    * ($point1['lat'] - $point2['lat'])
                    + cos($point1['lat'] / $deg_per_rad)  // Convert these to
                    * cos($point2['lat'] / $deg_per_rad)  // radians for cos()
                    * ($point1['long'] - $point2['long'])
                    * ($point1['long'] - $point2['long'])
            ) / 180);
    
        return $distance;  // Returned using the units used for $radius.
    }
    
    0 讨论(0)
  • 2020-11-28 05:05

    You can also try hitting a web service to calc the distance. Let someone else do the heavy lifting.

    https://www.zipcodeapi.com/API#distance

    0 讨论(0)
  • 2020-11-28 05:08

    In your zip table you need to grab the coordinates (lat,long) for the two points for which you want to get the distance.

    You can then either calculate the distance right in the SQL or with PHP. Both methods are outlined in this post:

    http://dev.strategystar.net/2011/10/mysql-php-get-distance-between-two-coordinates-in-miles-kilometers/

    The calculation in the example is based on the formula already discussed in this thread. It provides the radius for the earth in both miles and kilometers so it will allow you to get the distance between two points in both units.

    The link above is great because the method for calculation does not include any magic numbers, just the radius of the earth!

    0 讨论(0)
  • 2020-11-28 05:09

    Check out the Haversine formula for calculating great circle distances between two points. Some more samples can be found here

    Haversine formula:

    • R = earth’s radius (mean radius = 6,371km)
    • Δlat = lat2− lat1
    • Δlong = long2− long1
    • a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
    • c = 2.atan2(√a, √(1−a))
    • d = R.c

    (Note that angles need to be in radians to pass to trig functions).

    0 讨论(0)
提交回复
热议问题