1

My OpenLayers map has multiple, vector layers, each with unique features. I need a way to find the closest feature to a location across all the layers, in the same way that getClosestFeatureToCoordinate does for a single layer. I cannot find any OL method that would allow me to do determine that.

1

You could add all the features to a single temporary source

var tempSource = new VectorSource();
layers.forEach(function(layer){
  testSource.addFeatures(layer.getSource().getFeatures());
});
var closest = tempSource.getClosestFeatureToCoordinate(coordinate);

or possibly more efficient to just add the closest in each layer

var tempSource = new VectorSource();
layers.forEach(function(layer){
  testSource.addFeature(layer.getSource().getClosestFeatureToCoordinate(coordinate));
});
var closest = tempSource.getClosestFeatureToCoordinate(coordinate);
| improve this answer | |
  • Gah, I was so close...using temp source works perfect. Thanks! – Jared Holmberg Nov 9 '19 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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