-1

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

3
  • 1
    What i would recommend, is to just use googles spherical distance api and not roll your own distance. I was playing around on your fiddle, and the distance function you have implemented is wrong. Markers were saying they were 2000km away when they were sitting ontop of each other.
    – Brenden
    Sep 10, 2020 at 16:47
  • What is the "desired result? Why do you expect that result?
    – geocodezip
    Sep 10, 2020 at 17:27
  • If I replace your function with computeDistanceBetween from the geometry library, it works as I expect.
    – geocodezip
    Sep 11, 2020 at 0:56

2 Answers 2

1

Using filter() you can reduce the array down to the places that are within range.

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;
}

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" }
]


const checkPoint = { lat: -42.5, lng: 147.4 };
const distance = 50;
const withInRange = centerPointArray.filter( function (centerPoint) {
  return arePointsNear(checkPoint, centerPoint, distance);
});

console.log(withInRange);

0

I think the solution is simple: just take the return false statement out of the for loop. This way, you will return false only if no point in the array was close enough. (right now, you return false if the first point is not close enough)

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;
    }
    return false;
}
1
  • Unfortunately it does not. I added a link to a JS Fiddle to help explain what is going on here.
    – ShaneOG97
    Sep 10, 2020 at 16:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.