Skip to main content
typoo
Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42

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.

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 and array nearestPoint contains five pairs [iPoint, dist], where iPoint is index of the point in features array in dist is distance of the point.

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 end array nearestPoint contains five pairs [iPoint, dist], where iPoint is index of the point in features array in dist is distance of the point.

Source Link
TomazicM
  • 27k
  • 23
  • 31
  • 42

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 and array nearestPoint contains five pairs [iPoint, dist], where iPoint is index of the point in features array in dist is distance of the point.