The Issue
I am currently storing the latitude
and longitude
values for a location using the MySQL POINT
field type in the format:
POINT(51.507351 -0.127758)
I have never before used this kind of field type, and therefore do not have any experience with the queries, and how to actually, efficiently, use the data stored.
My Research
I have found many links that demonstrate various methods to search for items within a specified radius. However, most of these are using independent latitude
and longitude
fields instead of using the MySQL spatial fields.
Please see the following:
- Fastest Way to Find Distance Between Two Lat/Long Points
- http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/
- Use MySQL spatial extensions to select points inside circle
My Question
I am trying to search for any records within a given radius (in metres). Based on the structure of my table, what is the best, and most efficient, method of searching my records and returning any items within the specified radius (circle not rectangle)?
This is what I have so far:
SELECT
*,
(
6373 * acos (
cos ( radians( PASSED_IN_LATITUDE ) )
* cos( radians( X(location) ) )
* cos( radians( Y(location) ) - radians( PASSED_IN_LONGITUDE ) )
+ sin ( radians( PASSED_IN_LATITUDE ) )
* sin( radians( X(location) )
)
) AS distance
FROM locations
HAVING distance < PASSED_IN_RADIUS
I took the above code from another answer but given this answer was posted 2 years ago, I have assumed it it out of date and therefore this may not be the most efficient method anymore...