I have the following table Cities:
ID(int),City(char),latitude(float),longitude(float).
Now based on a user`s longitude(ex:44.8) and latitude(e
After some years from the accepted answer, it is possible to add some enhancements to the query:
Oracle database in version 11.1 added the function calc_distance (http://psoug.org/reference/functions.html), useful to calculate accurately the distance.
About the clauses to make the query faster, uses the conversion constant from distance to radians that varies with latitude (http://www.longitudestore.com/how-big-is-one-gps-degree.html) and adds an error that increases with search radius.
Here my changes that uses an average value of earth radius, in my tests it seems to be more accurate for large radius searches, in europe latitudes:
SELECT id, city, LATITUDE, LONGITUDE, distance FROM
(
SELECT id,
city,
LATITUDE, LONGITUDE,
calc_distance(LATITUDE, LONGITUDE, mylat, mylng) AS distance,
b.mydst
FROM Cities
JOIN (
SELECT :LAT AS mylat,
:LONG AS mylng,
:RADIUS_LIMIT AS mydst,
3.1415926 AS pi, -- or use pi() function if available
6371.4 earthradius
FROM DUAL
)b ON (1 = 1)
WHERE LATITUDE >= mylat - ((mydst / earthradius) * (180 / pi))
AND LATITUDE <= mylat + ((mydst / earthradius) * (180 / pi))
AND LONGITUDE >= mylng - ((mydst / earthradius) * (180 / pi) / cos(mylat * pi/180))
AND LONGITUDE <= mylng + ((mydst / earthradius) * (180 / pi) / cos(mylat * pi/180))
)a
WHERE distance <= mydst
ORDER BY distance