Skip to content

Instantly share code, notes, and snippets.

@moshmage
Last active April 18, 2022 09:22
  • Star 21 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save moshmage/2ae02baa14d10bd6092424dcef5a1186 to your computer and use it in GitHub Desktop.
compares two objects lat/lon and returns true if within provided kms
/**
* is One Point within Another
* @param point {Object} {latitude: Number, longitude: Number}
* @param interest {Object} {latitude: Number, longitude: Number}
* @param kms {Number}
* @returns {boolean}
*/
function withinRadius(point, interest, kms) {
'use strict';
let R = 6371;
let deg2rad = (n) => { return Math.tan(n * (Math.PI/180)) };
let dLat = deg2rad(interest.latitude - point.latitude );
let dLon = deg2rad( interest.longitude - point.longitude );
let a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(point.latitude)) * Math.cos(deg2rad(interest.latitude)) * Math.sin(dLon/2) * Math.sin(dLon/2);
let c = 2 * Math.asin(Math.sqrt(a));
let d = R * c;
return (d <= kms);
}
@bolorundurowb
Copy link

is there any reason why you chose to use Math.tan(n * (Math.PI/180)) instead of the usual n * (Math.PI/180)? The version without the Math.tan seems to be more accurate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment