0

I'm having trouble filling in the missing gaps of this OpenLayers 5 example: https://openlayers.org/en/latest/examples/select-multiple-features.html?q=select

there's a selected array that they push to, but I don't know what that's supposed to represent.

Currently I'm trying something like:

    const selected = select.getFeatures() // open layers select api

    map.forEachFeatureAtPixel(e.mapBrowserEvent.pixel, function (f) {
      let featureExists = false
      selected.forEach((feature) => {
        if (getFeatureId(feature) === getFeatureId(f)) {
          featureExists = true
        }
      })

      if (featureExists) {
        // remove
        selected.remove(f)
      } else {
        selected.push(f)
      }
    })

Trying to substitute the selected array in the example with the array of selected features from the Select api.

This code exists in this select handler callback: select.on('select', runSelectCallback)

I know that in the example they put the event handler on the map.on but I figured it shouldn't matter.

Problem is the feature will always exist because once I select a feature the Select interaction will have already added that item to it's array of features. So when I loop through the array which is pointless because there's always one item, it'll match with the feature at the map.pixel.

How do I select multiple items like in the example on a single click WITHOUT a toggle condition.

Your Answer

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

Browse other questions tagged or ask your own question.