cley cley - 4 months ago 14x
Javascript Question

How to find the nearest points to given coordinates

How do i identify if a point with a LAT, LONG coordinate format is near to other point.

Lets Say i want to find all the points near to:

-38.9086621 ,-68.082214

That are at less than 1km of distance. I already know how to find a coordinate in a given quadrant but not how to find a quadrant near a point.


You can use the haversine formular to calculate distances between two given coordinates. The calculated distance is the direct connection between the given coordinates (beeline).

JavaScript example (Source):

var R = 6371e3; // meters
var lat1Radians = lat1.toRadians();
var lat2Radians = lat2.toRadians();
var deltaLat = (lat2-lat1).toRadians();
var deltaLon = (lon2-lon1).toRadians();

var a = Math.sin(deltaLat/2) * Math.sin(deltaLat/2) +
        Math.cos(lat1Radians) * Math.cos(lat2Radians) *
        Math.sin(deltaLon/2) * Math.sin(deltaLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;