Created
March 2, 2011 16:55
-
-
Save robotmay/851250 to your computer and use it in GitHub Desktop.
SQL query to search based on latitude, longitude, and a distance in miles from a location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using an example table of 'addresses' with a latitude and longitude column | |
# Replace #LONGITUDE#, #LATITUDE#, and #DISTANCE_IN_MILES# with your search values | |
SELECT addresses.*, (ACOS( SIN(RADIANS(#LATITUDE#)) * SIN(RADIANS(addresses.latitude)) + COS(RADIANS(#LATITUDE#)) * COS(RADIANS(addresses.latitude)) * COS(RADIANS(addresses.longitude) - RADIANS(#LONGITUDE#)) ) * 3963.1676) AS distance | |
FROM addresses | |
WHERE (((ACOS( SIN(RADIANS(#LATITUDE#)) * SIN(RADIANS(addresses.latitude)) + COS(RADIANS(#LATITUDE#)) * COS(RADIANS(addresses.latitude)) * COS(RADIANS(addresses.longitude) - RADIANS(#LONGITUDE#)) ) * 3963.1676) <= #DISTANCE#) OR (addresses.latitude = #LATITUDE# AND addresses.longitude = #LONGITUDE#)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment