如何在开放层中显示矢量的几个特征?

时间:2020-06-09 17:48:40

标签: openlayers

我正在构建一个Web地图,其中有些图层是从Geoserver导入的。我已经将它们转换为向量JSON。我想在HTML面板中的地图外部显示几何的所有特征。

我只能使用方法.get拥有的一个功能'obra_nro'来做到这一点。您可以在下面查看我正在使用的代码。

但是,我仍然找不到显示其余功能的方法。

预先感谢您的任何建议。

var selectInteraction = new ol.interaction.Select({
        hitTolerance:1  ///// Precision del puntero del mouse para seleccionar
    });
olMap.getInteractions().extend([selectInteraction]);

    var displayFeatureInfo = function(pixel) {
        var features = [];
        olMap.forEachFeatureAtPixel(pixel, function(feature, layer) {
        features.push(feature);
        });
        var container = document.getElementById('description');
        if (features.length > 0) {
        var info = [];
        for (var i = 0, ii = features.length; i < ii; ++i) {
        info.push(features[i].get('obra_nro'));
        }
        container.innerHTML = info.join(', ') || '(unknown)';
        } else {
        container.innerHTML = '&nbsp;';
        }   
    };

    olMap.on('click' , function(evt) {
        var pixel = evt.pixel;
        displayFeatureInfo(pixel);
    });

2 个答案:

答案 0 :(得分:0)

例如,您可以调用forEachFeatureIntersectingExtent https://openlayers.org/en/latest/apidoc/module-ol_source_Vector-VectorSource.html#forEachFeatureIntersectingExtent并使用每个矢量源上的视图范围

var features = [];
olMap.getLayers().forEach(function(layer) {
  var source = layer.getSource();
  if (source && source.forEachFeatureIntersectingExtent) {
    source.forEachFeatureIntersectingExtent(olMap.getView().calculateExtent(), function(feature) {
      features.push(feature);
    });
  }
});

答案 1 :(得分:0)

我们和一位朋友找到了解决该问题的合理方法。我们保留了原始功能,但我们修改了“ if”部分。

var selectInteraction = new ol.interaction.Select({
        hitTolerance:1  ///// Precision del puntero del mouse para seleccionar
    });
    olMap.addInteraction(selectInteraction);

    olMap.on('singleclick', function(e) {
        var feature = olMap.forEachFeatureAtPixel(e.pixel, function(feature) {
            return feature;
        });
        var infoElement = document.getElementById('description');
        var html = "";
        if(feature){
            html += feature.get('obra_nro')+"</br>";
            html += feature.get('contratist')+"</br>";
            html += feature.get('mes1')+"</br>";
            html += feature.get('mes2')+"</br>";
            html += feature.get('mes3')+"</br>";
            html += feature.get('mes4')+"</br>";
        }
        infoElement.innerHTML = html;
    });
相关问题