Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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);

This comment has been minimized.

Copy link

bolorundurowb commented Aug 4, 2018

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
You can’t perform that action at this time.