1

I am using ol-ext to add a seach field for countries to my map like this:

map.addControl(new ol.control.SearchFeature({
  source: countriesSource, // of type ol.source.Vector
  property: "Name"
  // etc
}));

ol.control.SearchFeature does not allow multiple sources as source, though, so if I want to not only search for countries but also rivers, how would I provide another source to be considered in the search?

1
  • 1
    You will need to create a combined source for the search. Use the addfeature events of each of the individual sources to add those features to the combined source.
    – Mike
    Apr 20, 2022 at 12:17

1 Answer 1

1

As Mike pointed out, I had to listen to the addfeature event on the respective sources and add the corresponding feature to another dummy source:

const searchSource = new ol.source.Vector({
  features: []
});

countriesSource.on("addfeature", function (e) {
  e.feature.set("featureType", "country");
  searchSource.addFeature(e.feature);
});

riversSource.on("addfeature", function (e) {
  e.feature.set("featureType", "river");
  searchSource.addFeature(e.feature);
});

map.addControl(new ol.control.SearchFeature({
  source: searchSource,
  getTitle: function (feature) {
    switch (feature.get("featureType")) {
      case "country":
        return feature.get("countryName");
      case "river":
        return feature.get("riverName");
    }
  },
  // etc
}));

Note that adding and reading the custom featureType is not necessary, it's just very handy if you want to differenciate between the original sources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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