Skip to content

Instantly share code, notes, and snippets.

@jonsherrard
Created September 21, 2012 15:11
Show Gist options
  • Save jonsherrard/3762079 to your computer and use it in GitHub Desktop.
Save jonsherrard/3762079 to your computer and use it in GitHub Desktop.
query.js
function rad2deg (angle) {
return angle * 57.29577951308232; // angle / Math.PI * 180
}
function deg2rad (angle) {
return angle * .017453292519943295; // (angle / 180) * Math.PI;
}
function get_events (input_lat, input_lon) {
lat = input_lat;
lon = input_lon;
rad = 3;
earth_radius = 6371;
max_lat = lat + rad2deg(rad/earth_radius);
min_lat = lat - rad2deg(rad/earth_radius);
max_lon = lon + rad2deg(rad/earth_radius/cos(deg2rad(lat)));
min_lon = lon - rad2deg(rad/earth_radius/cos(deg2rad(lat)));
lat = deg2rad(lat);
lon = deg2rad(lon);
sql = "SELECT *, acos(sin(lat)*sin(radians(business_latitude)) + cos(lat)*cos(radians(business_latitude))*cos(radians(business_longitude)-lon))*earth_radius As distance
FROM (
SELECT *
FROM businesses
WHERE business_latitude>min_lat AND business_latitude< max_lat
AND business_longitude>min_lon AND business_longitude<max_lon
) As FirstCut
Where acos(sin(lat)*sin(radians(business_latitude)) + cos(lat)*cos(radians(business_latitude))*cos(radians(business_longitude)-lon))*earth_radius < rad
GROUP BY business_id;";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment