Openlayers:如何 select 根据功能使用哪种样式的弹出窗口?

Openlayers: How to select which style of popover should be used depending on the feature?

我是 OpenLayers 的新手,所以如果这个问题看起来很基础,请原谅我。

我想要的: 一张地图,其中显示了一些代表建筑物的标记。根据建筑物的类型,可以有两种不同类型的标记。 用户可以点击它们。单击时,弹出窗口会出现在标记顶部并显示有关该建筑物的信息。诀窍是弹出窗口的样式和显示的信息取决于标记的类型。

我在哪里:为了实现这一点,我创建了两个不同的 ol.layer.Vector,每个包含多个 ol.Feature。 然后我创建了两个 ol.Overlay 对应于两种不同类型的建筑物,以及一个 ol.interaction.Select。 标记正确显示在地图上。

我面临的问题:我不知道如何select根据点击的功能应该使用哪种弹出窗口样式。我试图创建两个 ol.interaction.Select 而不是一个,但实际上只使用了最后一个。

代码:

var elementFiche = document.getElementById('popup_fiche');
var elementMap = document.getElementById('popup_map');

//Overlay for the first kind of building
var overlay = new ol.Overlay({
    element: elementFiche,
    positioning: 'bottom-center',
    stopEvent: false,
    offset: [0, -50]
});
map.addOverlay(overlay);

//Overlay for the second kind of building
var overlayMap = new ol.Overlay({
    element: elementMap,
    positioning: 'bottom-center',
    stopEvent: false,
    offset: [-2, -10]
});
map.addOverlay(overlayMap);

var selectInteraction = new ol.interaction.Select({
    layers: [vectorLayer,vectorLayerMap],  
});
map.addInteraction(selectInteraction);

selectInteraction.getFeatures().on('add',function(e)
{
//re-position the overlay
    var feature = e.element;
    var point = feature.getGeometry();
    var coords = point.getCoordinates();

    //THIS IS WHERE I SHOULD SELECT THE OVERLAY
    //The following is an example for the first overlay. 
    overlay.setPosition(coords);

    //recreate the popover for the overlay
    var element = overlay.getElement();
    $(element).popover('destroy');
    $(element).popover({
      placement: 'top',
      animation: false,
      template: '<div class="popover" style="height: 150px; width: 500px;"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><a href="#" id="popup-closer" class="ol-popup-closer"></a><div class="popover-content"><p></p></div></div></div>',
      html: true,
      content: '<h4> <strong>'+feature.get('name')+'</strong><br> </h4>'+'<strong> Adresse : </strong>'+feature.get('adresse') + '<br>' +'<strong> Surface : </strong>'+feature.get('surface')+ '<br>' + '<strong> Année de construction : </strong>'+feature.get('dateConstruction')
    });
    $(element).popover('show');
  });

selectInteraction.getFeatures().on('remove', function(e){
    //destroy the popover
    $(overlay.getElement()).popover('destroy');
});  

我没有包含其余代码,因为我认为没有必要理解我的问题,但如果需要,请索取! 任何帮助将不胜感激。

谢谢!

我找到了解决方法:) 我刚刚为每个功能添加了一个 "property"(我称之为类型)以区分它们:

var iconFeature = new ol.Feature({
      geometry: new  
        ol.geom.Point(ol.proj.transform({{map.adresseGps}}, 'EPSG:4326',   'EPSG:3857')),
      libelle: "{{map.libelle}}",
      type: "mapping",
});

然后我只需要比较特征的类型:

  selectInteraction.getFeatures().on('add',function(e)
{
//re-position the overlay
var feature = e.element;
var point = feature.getGeometry();
var coords = point.getCoordinates();

var type = feature.get('type');

if(type == "ficheSite")
{
  overlayFiche.setPosition(coords);

  //recreate the popover for the overlay
  var element = overlayFiche.getElement();
  $(element).popover('destroy');
  $(element).popover({
    placement: 'top',
    animation: false,
    template: '<div class="popover" style="height: 150px; width: 500px;"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><a href="#" id="popup-closer" class="ol-popup-closer"></a><div class="popover-content"><p></p></div></div></div>',
    html: true,
    content: '<h4> <strong>'+feature.get('name')+'</strong><br> </h4>'+'<strong> Adresse : </strong>'+feature.get('adresse') + '<br>' +'<strong> Surface : </strong>'+feature.get('surface')+ '<br>' + '<strong> Année de construction : </strong>'+feature.get('dateConstruction')
  });

  $(element).popover('show');

}

else
{
  var size = feature.get('features').length;
  if(size == 1 )
  {
    var feature = feature.get('features')[0];
    overlayMap.setPosition(coords);

      //recreate the popover for the overlay
    var element = overlayMap.getElement();
    $(element).popover('destroy');
    $(element).popover({
      placement: 'top',
      animation: false,
      template: '<div class="popover" style="height: 100px; width: 500px;"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><a href="#" id="popup-closer" class="ol-popup-closer"></a><div class="popover-content"><p></p></div></div></div>',
      html: true,
      'content': '<h4> <strong> Instrument de Mesure </strong><br> </h4>'+'<strong> Libelle : </strong>'+feature.get('libelle')
    });
    $(element).popover('show');
  }

}
});