Last active
April 18, 2022 09:22
-
-
Save moshmage/2ae02baa14d10bd6092424dcef5a1186 to your computer and use it in GitHub Desktop.
compares two objects lat/lon and returns true if within provided kms
This file contains hidden or 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
/** | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there any reason why you chose to use
Math.tan(n * (Math.PI/180))
instead of the usualn * (Math.PI/180)
? The version without theMath.tan
seems to be more accurate