Use geokdbush for nearest neighbor searches
mainTo use geokdbush, you must first create a spatial index using kdbush. Once the index is finished, you can use geokdbush.around() to find the closest points to a specific longitude and latitude. The search accounts for Earth's curvature and date line wrapping.
Note that geokdbush works with the IDs returned by the kdbush index, which you can use to map back to your original data points.
import KDBush from 'kdbush';
import * as geokdbush from 'geokdbush';
const index = new KDBush(points.length);
for (const {lon, lat} of points) index.add(lon, lat);
index.finish();
const nearestIds = geokdbush.around(index, -119.7051, 34.4363, 1000);
const nearest = nearestIds.map(id => points[id]);