I found an answer here that determines if two points are near each other based on a certain radius:
Check if a latitude and longitude is within a circle google maps
This is not the exact answer I am looking for however as I want to rewrite this function:
function arePointsNear(checkPoint, centerPoint, km) {
var ky = 40000 / 360;
var kx = Math.cos(Math.PI * centerPoint.lat / 180.0) * ky;
var dx = Math.abs(centerPoint.lng - checkPoint.lng) * kx;
var dy = Math.abs(centerPoint.lat - checkPoint.lat) * ky;
return Math.sqrt(dx * dx + dy * dy) <= km;
}
To be able to check if the point of interest is within a certain distance of any other point in the array. Here is my attempt so far but does not work as expected:
function arePointsNear(checkPoint, centerPointArray, km) {
var ky = 40000 / 360;
for (var i = 0; i < centerPointArray.length; i++) {
var kx = Math.cos(Math.PI * centerPointArray[i].lat / 180.0) * ky;
var dx = Math.abs(centerPointArray[i].lng - checkPoint.lng) * kx;
var dy = Math.abs(centerPointArray[i].lat - checkPoint.lat) * ky;
if (Math.sqrt(dx * dx + dy * dy) <= km) {
return true;
} else {
return false;
}
}
}
var centerPointArray = [
{ lat: -42.734358, lng: 147.439506, info: "T" },
{ lat: -42.735258, lng: 147.438000, info: "V" },
{ lat: -43.999792, lng: 170.463352, info: "W" }
]
Here is a link to the JS Fiddle:
https://jsfiddle.net/ohb4puj2/3/
Upon opening up the debugger it does not return the desired result when the markers are moved to within a 10KM radius of each other
computeDistanceBetween
from the geometry library, it works as I expect.