I’ve got an sql query that pulls locations from a database based on coordinates within a certain range. Now I’m trying to order them by proximity with a bit of math – but no matter how many times im writing it out on paper I can’t seem to figure out a working solution.
lets describe a few variables:
- $plat (the latitude of the place)
- $plong (the longitude of the place)
- $slat (the latitude of the searched location)
- $slong (the longitude of the searched location)
One big thing to note is that I’ve converted all coordinates to positive numbers and have another feild that determines positive or negative – but for this assume that I’ve already queried the nearby coordinates properly.
In order to order it this way I’ve seen people use “ORDER BY abs(coord-coords)” for something simple -but what I need is something more like:
[($slat – $plat) * ($slong – plong)] – but this causes problems because if the resulting calulation on one side is zero – then after multiplied the result would be zero as well – making it innaccurate.
Any ideas –
The solution for distance between latitude/longitude coordinates is called the haversine formula. It’s complex because you need to take the curvature of the earth into account unless your distances are very short.
Here’s an article about using PHP and MySQL to implement a locator app: Creating a Store Locator with PHP, MySQL & Google Maps
You can also find many other questions here on Stack Overflow regarding calculating distance between coordinates: https://stackoverflow.com/search?q=longitude+distance
If you only need to calculate distance within 5km, the curvature of the earth is probably not significant. You can use a plain distance formula. You can even skip the square-root calculation if you only need to use this value to sort which one is closer.
No, those are PHP variables. You need to calculate the distance from the lat/long coordinates on each row of your database table.
The problem is that SQL normally only calculates things from one row at a time. So you need to have some way of combining two rows into one row, so a calculation can use the coordinates of two locations. You do this with a self-join. This is basically pairing each row with another row from the same table. And that’s why I list
Locationstwice, and give them two different aliases,sandp(the technical term is correlation name).If you’re accustomed to PHP, think of this self-join as analogous to a nested loop:
The
WHEREclause restricts the rows ofpto just the place you start from (you would substitute a single value for the ? placeholder), so it’s just one row that the query pairs with all the rows of the table.Another tip: I skipped using
SQRT()because it’s not necessary just to sort by distance. That is, if three locations are 10km, 15km, and 20km from me, then I can sort by 100, 225, and 400 and get the same ordering as if I sort by 10, 15, and 20. The advantage is that I’ve eliminated calculating the square root which reduces the cost of my query somewhat.