5

Is there a way to have a selectInteraction in OpenLayers 3 that only allows one feature to be selected at a time?

Jose Gómez
  • 3,110
  • 2
  • 32
  • 54
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196

3 Answers3

14

define select interaction as follow:

select = new ol.interaction.Select (
    toggleCondition: ol.events.condition.never
)
Morteza Malvandi
  • 1,656
  • 7
  • 30
  • 73
Paul
  • 141
  • 2
  • 1
    Fix the answer, it should be `select = new ol.interaction.Select ({ toggleCondition: ol.events.condition.never })` – Thomas Gratier Jan 23 '17 at 14:01
  • If anyone is using VueLayers, I got this to work by putting `import { never } from 'ol/events/condition'` at the top of the scrpt and then defining it in the `data` block as `never` and then specifying `:toggle-condition="never"` in the `` component. – wfgeo Mar 29 '22 at 12:39
2

Not really sure I understood well.

If you go to the API doc for OpenLayers 3.4 and untick Stable Only on the page top right, you will see a multi option to manage this. Normally, according to the doc, what you are asking is already by default.

Multi A boolean that determines if the default behaviour should select only single features or all (overlapping) features at the clicked map position. Default is false i.e single select

Thomas Gratier
  • 2,311
  • 1
  • 14
  • 15
  • 4
    Sorry for the confusion. The `multi` option is for selecting multiple features with one click. I want to turn off the ability to hold the `shift` key and select multiple features. – Tyler DeWitt May 04 '15 at 13:41
1

For OpenLayers4 this can easily be done by combining conditions.

For example a condition that only selects on singleClick and not when shiftKey is pressed:

const select = new Select({
  condition: (mapBrowserEvent) => {
    return singleClick(mapBrowserEvent) && !shiftKeyOnly(mapBrowserEvent)},
  layers: [layer]
});

Find more conditions in the API doc: http://openlayers.org/en/latest/apidoc/module-ol_events_condition.html