Openlayers 4触发地图上的选择功能

时间:2018-10-08 12:10:23

标签: javascript openlayers

我有一个网站,您在哪里有地图以及地图中的区域列表。

当您单击该区域时,我在地图上添加了选择事件。

// 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

当然,当选择功能更改颜色时。

单击列表时如何触发同一事件?

  $('<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
});

1 个答案:

答案 0 :(得分:0)

您是否只是尝试过将功能推入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);