MySQL calculate the distance between two longitude and latitude, including specific SQL statements.

/*The incoming parameters are latitude latitude longitude ASC ascending order from near to far DESC descending order from far to near * /

SELECT
    *,
    ROUND(
        6378.138 * 2 * ASIN(
            SQRT(
                POW(
                    SIN(
                        (
                            40.0497810000 * PI() / 180 - lat * PI() / 180
                        ) / 2
                    ),
                    2
                ) + COS(40.0497810000 * PI() / 180) * COS(lat * PI() / 180) * POW(
                    SIN(
                        (
                            116.3424590000 * PI() / 180 - lon * PI() / 180
                        ) / 2
                    ),
                    2
                )
            )
        ) * 1000
    ) AS juli
FROM
    customer
ORDER BY
    juli ASC

  

lat, lonFor latitude and longitude

phpCode

//The distance A ($lat1, $lng1) and B ($lat2, $lng2) are calculated according to latitude and longitude.
function getDistance($lat1,$lng1,$lat2,$lng2) {
    //Radius of the earth
    $R = 6378137;
    //Turning angle to Fox degree
    $radLat1 = deg2rad($lat1);
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    //Result
    $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;
    //accuracy
    $s = round($s* 10000)/10000;
    return  round($s);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *