【问题标题】:Showing name of feature after click on VECTOR LAYER单击矢量层后显示特征名称
【发布时间】:2016-03-03 11:57:25
【问题描述】:

我是 Openlayers 3 的新手。我有从 geojson 文件导入的矢量图层。单击矢量图层后,我想显示有关我的功能的信息。 知道我该怎么做吗?

【问题讨论】:

    标签: vector geojson openlayers-3


    【解决方案1】:

    为此,我使用了来自 here 的库。示例代码是

    var popup = new ol.Overlay.Popup();
    map.addOverlay(popup);
    
    //handling Onclick popup
    map.on('click', function(evt) {
     var feature = map.forEachFeatureAtPixel(evt.pixel,
       function(feature, layer) {
        return feature;
      });
     if (feature) {
        var coord = event.feature.getGeometry().getCoordinates();
        popup.show(coord, '<div><h2>Tilte</h2><p>' +feature.get('<property_in_single_quotes>')+ '</p></div>');  
    }
    });
    

    希望对你有帮助

    【讨论】:

    • 谢谢,它有效!我是这样写的:map.on('click', function(e) { var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; }); var infoElement = document.getElementById('info '); infoElement.innerHTML = feature ? feature.get('gml_id') : ''; 我只想显示更多功能,而不仅仅是 'gml_id' 。我应该怎么写?
    • 我认为你可以使用 jquery append() 来追加 feature.get('')
    • 我必须把 append() 放在哪里?
    • 类似下面的 sn-p $("#info").append('&lt;b&gt;Title&lt;/b&gt; : '+feature.get('gml_id')+'&lt;br/&gt; &lt;b&gt;Title&lt;/b&gt; : '+feature.get('gml_id_2'))
    • instend of infoElement.innerHTML = feature ?特征.get('gml_id') : '';使用这个 $("#info").append('Title : '+feature.get('gml_id')+'
      Title : '+feature .get('gml_id_2')) .
    【解决方案2】:

    看看这些例子:

    1) http://openlayers.org/en/v3.14.1/examples/vector-layer.html?q=overlay

    2) http://openlayers.org/en/v3.14.1/examples/popup.html?q=overlay

    您没有将矢量信息放在地图旁边,而是将其放在您创建的弹出窗口&lt;div&gt; 中。

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 2018-04-07
      • 2021-06-04
      • 1970-01-01
      • 2018-02-11
      • 2018-11-16
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多