hskrijelj
hskrijelj

Reputation: 433

Sorting zip code proximity search by distance (php/mysql)

I have a table (user_zip_codes) with the users' zip_code, latitude and longitude. I have found a function here on stackoverflow that finds zip codes within a specific radius:

function zipcodeRadius($lat, $lon, $radius) {
    global $mysqli;
    $zipcodeList = array();
    $radius = ($radius ? $radius : 20);
    $sql = "SELECT userID,city,zip_code,country FROM user_zip_codes  WHERE (3958*3.1415926*sqrt((lat-$lat)*(lat-$lat) + cos(lat/57.29578)*cos($lat/57.29578)*(lon-$lon)*(lon-$lon))/180) <= $radius GROUP BY zip_code";
    if($stmt = $mysqli->prepare($sql)) {
        $stmt->execute();
        $stmt->bind_result($userID,$city,$zip_code,$country);
        while($stmt->fetch()) {
            $zipcodeList[] = array('userID'=>$userID,'city'=>$city,'zip_code'=>$zip_code,'country'=>$country);
        }
    }
    return $zipcodeList;
}

It works perfectly. However, I would like the function to sort the array by distance (either by ASC og DESC). How should I adjust my query in order for this to happen?

Thanks in advance.

UPDATE: The word 'distance' might appear ambiguous (thanks to Jorge). I simply wish to sort the zip_codes by distance meant as the distance between two points.

Upvotes: 2

Views: 2557

Answers (3)

Olli
Olli

Reputation: 1738

You could use something like

$iDistance = 20;
$iRadius = 6371; // earth radius in km
$iRadius = 3958; // earth radius in miles
$fLat = x.y; // Your position latitude
$fLon = x.y; // Your position longitude

$strQuery = "
SELECT 
  *, 
  $iRadius * 2 * ASIN(SQRT(POWER(SIN(( $fLat - abs(pos.lat)) * pi() / 180 / 2),2) +
COS( $fLat * pi()/180) * COS(abs(pos.lat) * pi() / 180) * POWER(SIN(( $fLon - pos.lon) *
pi() / 180 / 2), 2) )) AS distance
FROM user_zip_codes pos
HAVING distance < $iDistance 
ORDER BY distance";

where you have to fetch your lat/lon value before using the SQL. This works for me

Upvotes: 4

ffflabs
ffflabs

Reputation: 17481

MySQL has geometric functions to calculate distance. However, you would need to manage your coordinate pairs as Point fields instead of separate float fields. A workaround would be to cast both columns into a coordinate pair.

$sql = "SELECT userID,
            city,
            zip_code,
            country, 
            GeometryFromText( CONCAT( 'POINT(', lon, ' ', lat, ')' ) ) as coord
        FROM user_zip_codes  ";

If instead of points you use your $lat and $lon variables to cast a LineString, yo could use GLength to get the distance

$sql = "SELECT userID,
                city,
                zip_code,
                country, 
                GLength(GeomFromText(CONCAT('LineString(',lon,' ', lat1,',$lon $lat)'))) as distance
            FROM user_zip_codes  
            where GLength(GeomFromText(CONCAT('LineString(',lon,' ', lat1,',$lon $lat)')))<=$radius
            ORDER BY distance DESC";

It still looks messy. There should be a way to declare a LineString without using its WKT representation. I'll keep looking.

Upvotes: 0

Strawberry
Strawberry

Reputation: 33935

ORDER BY (3958*3.1415926*sqrt((lat-$lat)*(lat-$lat) + cos(lat/57.29578)*cos($lat/57.29578)*(lon-$lon)*(lon-$lon))/180) DESC

Upvotes: 1

Related Questions