Question

I build a small simple script in php / mysql to check how far the distance is between two cities in Holland. The problem is that i want to know how i can calculate the max distance in the ratio of whatever is given.

Example, i'm located in Sittard and i check a few cities in the neighbourhood.

Van Sittard (51.00000000,5.86666700) naar Munstergeleen     (50.98333300,5.86666700) is 1.9 KM
Van Sittard (51.00000000,5.86666700) naar Doenrade      (50.96666700,5.90000000) is 4.4 KM
Van Sittard (51.00000000,5.86666700) naar Geleen        (50.96666700,5.83333300) is 4.4 KM
Van Sittard (51.00000000,5.86666700) naar Nuth          (50.91666700,5.88333300) is 9.3 KM
Van Sittard (51.00000000,5.86666700) naar Hoensbroek        (50.91666700,5.93333300) is 10.4 KM

I know my location, well at least my city and the location i want to go to. Now i want to know howmuch lets say 10km is.

So i can do something like

SELECT * FROM locations WHERE mylocation > whevereveriwantogoto AND whereeveriwanttogoto > maxmylocation ?

There should be a easy way (i guess) to just do in a simple query the ma radius from an given gps location? Otherwise i need to check every single gps location in the database, see if they are within the radius and then only display those. Lets say i have 1 million gps locations in my database, my script is used by 1000 people.

I don't even want to know how much the load of my server is going to be ;)

Anyone knows a simple mysql query?

Small update i tried this:

$lat = $_GET['lat'];
$lng = $_GET['lng'];

$sql="SELECT *, (3959 * acos(cos(radians('".$lat."')) * cos(radians(lat)) * cos( radians(lng) - radians('".$lng."')) + sin(radians('".$lat."')) * 
sin(radians(lat)))) 
AS distance 
FROM postcode WHERE distance < 15 ORDER BY distance LIMIT 0 , 10";

$result = mysql_query($sql);
while($r = mysql_fetch_assoc($result)) {
echo $r['name'];
}

but i get:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'long) - radians('5.86666700')) + sin(radians('51.00000000')) * sin(radians(lat))' at line 1

I updated again but now i get:

9061 8882 9174 6285 4931 7667 8458 7135 5534

Those zipcodes are not even closeby..

I use: lat and lng in the database for location

So the question is, how can i get how far around point a it can be? in example codes i see 'distance' but i don't have that table and i have no clue why i need it. Distance from where?

No correct solution

OTHER TIPS

Fixed it!

Example:

$lat = $_GET['lat'];
$lng = $_GET['lng'];
$distance = $_GET['dist'];

$result = mysql_query("SELECT *, ( 6371 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin(radians(lat)) ) ) AS distance FROM city HAVING distance < $distance ORDER BY distance");
echo mysql_error();
while($r = mysql_fetch_assoc($result)) {

$getcityname = mysql_query("SELECT * FROM cityname WHERE city_id = '$r[id]' AND official = '1'");
while($putcityname = mysql_fetch_array($getcityname)) {
$cityname = $putcityname['name'];
}
echo $cityname;
echo "<br />";
}
select 111.1111 * 
DEGREES(
    ACOS(
        COS(RADIANS(lat1)) * COS(RADIANS(lat2)) * COS(RADIANS(lng1 - lng2)) + 
        SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
    )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top