This question already has an answer here:
I have $latitude = 29.6815400
and $longitude = 64.3647100
, now in MySQL I would like to take the 15 nearest places to these coordinates and I'm planning to do this query:
SELECT *
FROM places
WHERE latitude BETWEEN($latitude - 1, $latitude + 1)
AND longitude BETWEEN($longitude - 1, $logintude + 1)
LIMIT 15;
Do you think it's correct or do you suggest something else?
How to do the BEETWEEN
, since I want to search trough a maximum of 50Km range the near places?
I forgot to say that I can also use PHP for do anything before to run the query.
Note: I can't use stored procedures.
You have to consider that flooding any DBMS like
MySQL
with heavy queries should not be the best solution.Instead you can speculate a very-fast SQL query selecting all the places with coordinates inside the simple square of side
$radius
, instead of selecting suddently a perfect circle radius.PHP
can filter the surplus.Let me show the simple concept:
So
MySQL
runs a very friendly query that does not require to read all the database set, andPHP
strips out the surplus with something similar to thegetDistanceBetweenPointsNew()
function posted in this page, comparing the distance from the coordinates of the result set to the center of your radius.In order to do not waste the (big) performance gain, index your coordinates columns in the database.
Happy hacking!
I've done something similar with a selling houses app, ordering by distance from a given point, place this in your SQL select statement:
Replace
$search_location
with your relevant lat/lng values and the map_lat/map_lng values are the SQL columns which contain the lat/lng values. You can then order the results by distance and either use a where or having clause to filter our properties within a 50km range.I would recommend using SQL as the approach compared to PHP in the event you require additional functionality such as paging.
A bit late but it may help someone - if you want the nearest city by location, I wouldn't go on distance because then an isolated location wouldn't retrieve anything. Try this:
here’s the PHP formula for calculating the distance between two points:
then add a query to get all the records with distance less or equal to the one above:
and you can take a look here for similar computations.
and you can read more here
Update:
you have to take in mind that to calculate longitude2 and longitude2 you need to know that:
Each degree of latitude is approximately 69 miles (111 kilometers) apart. The range varies (due to the earth's slightly ellipsoid shape) from 68.703 miles (110.567 km) at the equator to 69.407 (111.699 km) at the poles. This is convenient because each minute (1/60th of a degree) is approximately one mile.
A degree of longitude is widest at the equator at 69.172 miles (111.321) and gradually shrinks to zero at the poles. At 40° north or south the distance between a degree of longitude is 53 miles (85 km).
so to calculate
$longitude2 $latitude2
according to 50km then approximately: