Openlayers 4 触发地图上的 select 特征

Openlayers 4 trigger select feature on map

我有一个网站,你在哪里有地图和地图中的区域列表。

我在点击区域时在地图上添加了 select 事件。

// add select features to the map
map.addInteraction(select);

var selectedFeatures = select.getFeatures();

selectedFeatures.on(['add', 'remove'], function() {
  var names = selectedFeatures.getArray();
  var areasel = [];
  names.forEach(function(feature) {
    areasel.push(feature.getProperties().Name);
    var theCode = feature.getProperties().Code;
    $('#bt'+ theCode).click();
  });
}); // end on select

当然,当它 selected 功能改变颜色时。

如何在点击列表时触发相同的事件?

  $('<p>', {
    text: '...'
  }).appendTo('#li' + s.code);
  $('<button />', {
    id: 'bt' + s.code,
    class: "custbtn live",
    text: "Live",
    url: s.url
  }).click(function(e) {

  // Which function do I need to parse?
  // At this point I can have the Name of the feature that i want to select 
  // s.name but how can I use it to trigger the event on the map?

  }).appendTo('#li' + s.code);

这是WFS矢量图层。

// UK map source
var vectorSource = new ol.source.Vector({
  format: new ol.format.WFS(),
  url: function(extent) {
    return 'http://www.trafficorders.uk/cgi-bin/mapserv?map=/var/www/vhosts/trafficorders.uk/httpdocs/maps/wfsareas.map&service=WFS&' +
      'version=1.1.0&request=GetFeature&typename=la_areas&' +
      'outputFormat=text/xml; subtype=gml/3.1.1&srsname=EPSG:27700';
  },
  strategy: ol.loadingstrategy.all
});

// UK map layer
var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  style: styleFunction
});

您是否只是尝试将此功能推送到您的 selectedFeatures 数组中?

var selectedFeatures = select.getFeatures();

$('<p>', {
    text: '...'
  }).appendTo('#li' + s.code);
  $('<button />', {
    id: 'bt' + s.code,
    class: "custbtn live",
    text: "Live",
    url: s.url
  }).click(function(e) {

      selectedFeatures.push(s); //if 's' is actually the feature you got from openLayer?

  }).appendTo('#li' + s.code);