如何枚举群集地图中的要素?

时间:2019-04-22 21:55:21

标签: openlayers

我正在基于开放层clustered features开发一个聚簇地图。 当用户单击群集时,我正在处理事件,但无法获得用户单击的群集的属性。

我的问题:当用户单击集群时,如何枚举集群中的功能并访问其属性?

我的代码如下:

var source = new VectorSource({
        features: features  //here are my features
});

var clusterSource = new Cluster({
        distance: 20,
        source: source
});

var clusters = new VectorLayer({
        source: clusterSource,
        style: function(feature) { ... }
});

var raster = new TileLayer({
        source: new OSM()
});

var map = new Map({
        layers: [raster, clusters],
        target: 'map',
        view: new View({ center: [0, 0], zoom: 2})
});

map.on('click', function(event) {
    map.forEachFeatureAtPixel(event.pixel, function(feature,layer) {
        alert('found feature');
        // here I am stuck. How can I access the properties in the features of the cluster ?
    });
});

1 个答案:

答案 0 :(得分:1)

感谢迈克,这是有效的代码:

map.on('click', function(event) {
    map.forEachFeatureAtPixel(event.pixel, function(feature,layer) {
        alert('found feature');
        var Clusters = feature.getProperties();
        var NumberOfFeaturesInCluster = Clusters.features.length

        //example of extracting and concatenating a property called 'name'
        var listofnames = ''
        for (i = 0; i < NumberOfFeaturesInCluster; i++) {
          listofnames += ", " + Clusters.features[i].getProperties().name;
        }

    });
});