i'm using openlayers v3.6 (this important, because of solutions found , potentialy work openlayers 2).
i have table , when select row in table, highlight/select corresponding feature on openlayers map. features simple polygons (ol.geom.polygon) in same vector layer (ol.layer.vector).
i set select interaction this:
// there lot of other code here ... addselectlistener: function() { this.selectinteraction = new ol.interaction.select({ condition: ol.events.condition.singleclick, layers: function (layer) { // defines layer features selectable return layer.get('id') == 'polygons_layer'; }, style: this.style.selected }); // map = ol.map map.addinteraction(this.selectinteraction); this.selectinteraction.on('select', this.selectpolygon, this); } ... selectpolygon: function(event) { var selectsrc = this.getselectinfo(event); // code relies on selectsrc data } ... getselectinfo: function (event) { var selectsrc = { deselected: null, selected: null, type: null }; if (event.selected.length == 0 && event.deselected.length == 1) { // click outside of polygon selected selectsrc.type = 'deselect'; selectsrc.deselected = { feature: event.deselected[0], id: event.deselected[0].getid() }; } else if (event.deselected.length == 0 && event.selected.length == 1) { // click on polygon without selected selectsrc.type = 'select'; selectsrc.selected = { feature: event.selected[0], id: event.selected[0].getid() } } else if (event.deselected.length == 0 && event.selected.length == 1) { // click on polygon selected selectsrc.type = 'switch'; selectsrc.deselected = { feature: event.deselected[0], id: event.deselected[0].getid() }; selectsrc.selected = { feature: event.selected[0], id: event.selected[0].getid() } } else { selectsrc.type = 'out'; } return selectsrc; } this functions when want select polygon clicking on on map. want achieve same, not clicking on map rather click on element outside map (table row in example, really).
i use select interaction because of event emitted , because of styling applies selected features. however, if chance can manipulate selected features in select interaction without having same event ok.
i'm aware of question & answer - openlayers 3: select feature programmatically - problem cannot ask in comments clarification (for example, myselectcontrol), because don't have reputation :)
the way in linked question. so, push ol.feature selected collection:
var select = new ol.interaction.select({ //some options }); map.addinteraction(select); var selected_collection = select.getfeatures(); selected_collection.push(featurepoint); if want trigger select event:
select.dispatchevent('select'); // or select.dispatchevent({ type: 'select', selected: [featurepoly], deselected: [] });
Comments
Post a Comment