It looks like L.GeometryUtil.nClosestLayers
method in this case always returns the first five elements from features
array (tested).
Since I could not quickly find the reason for this behaviour, here is one possible solution for finding first five closest points:
var i, j;
var distance;
var nearestPoint = [];
for (i = 0; (i < features.length); i++) {
distance = map.distance(features[i].geometry.coordinates, nothernmostLayer.geometry.coordinates);
if (distance == 0) continue;
if (nearestPoint.length == 0) {
nearestPoint[0] = [i, distance];
}
else {
for (j = 0; (j < nearestPoint.length); j++) {
if (distance < nearestPoint[j][1]) {
nearestPoint.splice(j, 0, [i, distance]);
if (nearestPoint.length > 5) {
nearestPoint.splice(5, 1);
};
break;
}
}
}
}
At the andend array nearestPoint
contains five pairs [iPoint, dist]
, where iPoint
is index of the point in features
array in dist
is distance of the point.