Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MySQL: Select locations close to a given location from DB

In PHP, I have the following code for calculating the distance between two locations:

<?php
function distance($lat1, $long1, $lat2, $long2) {
    // DEGREE TO RADIAN
    $latitude1 = $lat1/180*pi();
    $longitude1 = $long1/180*pi();
    $latitude2 = $lat2/180*pi();
    $longitude2 = $long2/180*pi();
    // FORMULA: e = ARCCOS ( SIN(Latitude1) * SIN(Latitude2) + COS(Latitude1) * COS(Latitude2) * COS(Longitude2-Longitude1) ) * EARTH_RADIUS
    $distance = acos(sin($latitude1)*sin($latitude2)+cos($latitude1)*cos($latitude2)*cos($longitude2-$longitude1))*6371;
    return $distance;
}
echo distance(9.9921962, 53.5534074, 9.1807688, 48.7771056); // Hamburg, DE - Stuttgart, DE
?>

But now, I want to select locations close to a given location via PHP from my MySQL database:

  • The user enters his hometown
  • My script gets the latitude/longitude values via the Google API
  • In my database, I have about 200 locations with a field for the latitude value and a field for the longitude value
  • I need a code for PHP and MySQL to select the 10 locations which are closest to the user's hometown

I hope you can help me. Thanks in advance!

like image 727
caw Avatar asked May 21 '09 19:05

caw


People also ask

How to get near BY location in PHP?

php"); $lat = "3.107685"; $lon = "101.7624521"; $sql="SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS 'distance' FROM loc_coordinate HAVING 'distance'<='10' ORDER BY 'distance' ASC"; $stmt =$ ...

How do I find the closest location using latitude and longitude in mysql?

To find locations in your markers table that are within a certain radius distance of a given latitude/longitude, you can use a SELECT statement based on the Haversine formula. The Haversine formula is used generally for computing great-circle distances between two pairs of coordinates on a sphere.

WHERE clause MySQL with PHP?

The WHERE Clause is used to filter only those records that are fulfilled by a specific condition given by the user. in other words, the SQL WHERE clause is used to restrict the number of rows affected by a SELECT, UPDATE or DELETE query.

WHERE clause used in PHP?

The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified condition.


2 Answers

MySQL Great Circle Distance (Haversine formula) does exactly what you need.

With only 200 records however you may as well just load them all and check them with code. The data set is really way too small to be worrying too much about database vs code or any other such optimizations.

Calculating distance between zip codes in PHP has a couple of PHP implementations of this algorithm.

Geo Proximity Search is pretty much the exact same problem you have.

like image 165
cletus Avatar answered Oct 27 '22 00:10

cletus


Maybe something like

SELECT field1, field2, ...,
    ACOS(SIN(latitude / 180 * PI()) * SIN(:1) + COS(latitude / 180 * PI()) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
    ORDER BY distance ASC;

or

SELECT field1, field2, ...,
    ACOS(SIN(RADIANS(latitude)) * SIN(:1) + COS(RADIANS(latitude)) * COS(:2) * COS(:2 - longtidude)) * 6371 AS distance
    ORDER BY distance ASC;

(directly translated from the PHP code)

:1 and :2 is $lat2/180*pi() and $long2/180*pi() respectively.

like image 41
itsbth Avatar answered Oct 26 '22 23:10

itsbth