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.
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);