mySQL select zipcodes within x km/miles within range of y

前端 未结 3 731
名媛妹妹
名媛妹妹 2020-12-17 07:11

Note: Although I use a zipcode database with Dutch zipcodes, this question is country independent.

I have a database with every zipcode in the Netherlands +

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 07:46

    You have to use something called the Haversine formula:

    $sql = "
        SELECT zipcode
        FROM zipcodes
        WHERE ".mysqlHaversine($lat, $lon, $distance)."
    ";
    

    And the formula:

    function mysqlHaversine($lat = 0, $lon = 0, $distance = 0)
    {
        if($distance > 0)
        {
            return ('
            ((6372.797 * (2 *
            ATAN2(
                SQRT(
                    SIN(('.($lat*1).' * (PI()/180)-latitude*(PI()/180))/2) *
                    SIN(('.($lat*1).' * (PI()/180)-latitude*(PI()/180))/2) +
                    COS(latitude * (PI()/180)) *
                    COS('.($lat*1).' * (PI()/180)) *
                    SIN(('.($lon*1).' * (PI()/180)-longitude*(PI()/180))/2) *
                    SIN(('.($lon*1).' * (PI()/180)-longitude*(PI()/180))/2)
                    ),
                SQRT(1-(
                    SIN(('.($lat*1).' * (PI()/180)-latitude*(PI()/180))/2) *
                    SIN(('.($lat*1).' * (PI()/180)-latitude*(PI()/180))/2) +
                    COS(latitude * (PI()/180)) *
                    COS('.($lat*1).' * (PI()/180)) *
                    SIN(('.($lon*1).' * (PI()/180)-longitude*(PI()/180))/2) *
                    SIN(('.($lon*1).' * (PI()/180)-longitude*(PI()/180))/2)
                ))
            )
            )) <= '.($distance/1000). ')');
        }
    
        return '';
    }
    

    Usually I do not use code without understanding the way it works first, but I must confess this function is a little bit over my head...

提交回复
热议问题