【问题标题】:why doesn't show feature on map为什么地图上没有显示特征
【发布时间】:2016-01-06 22:22:23
【问题描述】:

这是小提琴Link。当它像这样的代码时,地图上不会显示地图:

 var featureVectorLayer = new ol.layer.Vector({
    source: featureClusterSource,
    style: new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.2)'
        }),
        stroke: new ol.style.Stroke({
            color: 'blue',
            width: 2
        }),
        image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({
                color: '#ffcc33'
            })
        })
    })
});

但我将源 - featureClusterSource 更改为 featureVectorSource。它运行良好,但此时我在地图上单击功能时没有获得功能。

 var featureVectorLayer = new ol.layer.Vector({
    source: featureVectorSource,
    style: new ol.style.Style({
        fill: new ol.style.Fill({
            color: 'rgba(255, 255, 255, 0.2)'
        }),
        stroke: new ol.style.Stroke({
            color: 'blue',
            width: 2
        }),
        image: new ol.style.Circle({
            radius: 7,
            fill: new ol.style.Fill({
                color: '#ffcc33'
            })
        })
    })
});

如何使用 featureClusterSource 在地图上显示要素?

【问题讨论】:

  • 以 featureVectorSource 作为源,您的代码对我有用。我什至得到了点击事件。
  • 是的,它适用于 featureVectorSource 。但它不适用于 featureClusterSource
  • 是否有不能继续矢量源的原因?
  • 是的,因为当我点击功能相交时,我只有一个功能。它不是真的。我必须同时获得两个功能
  • 为此,您必须更改 forEachFeatureAtPixel 方法。根据您的代码,该方法会在检测到功能后立即返回该功能。所以顶部的任何功能都会被返回。

标签: javascript openlayers-3


【解决方案1】:

但我将源 - featureClusterSource 更改为 featureVectorSource。它运行良好,但此时我在地图上单击功能时没有获得功能。

单击相交的要素时,更改 forEachFeatureAtPixel 方法以将它们添加到数组中。 Here 是一个工作小提琴

    map.on("singleclick", singleClickCB);
    function singleClickCB(event) {
        var features = [];
        map.forEachFeatureAtPixel(event.pixel, function(feature, layer) {
          features.push(feature);
        });
        if (features) {
           var i;
           for (i = 0; i < features.length; ++i) {
              alert(features[i].get('title'));
            }
         }
   } ;

【讨论】:

猜你喜欢
  • 2016-08-31
  • 2017-02-14
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2013-02-17
相关资源
最近更新 更多