mapbox-developer-group / Mapbox-Tech-Q-A

在提问之前请自行查看文档哦,养成好习惯:https://docs.mapbox.com/
32 stars 1 forks source link

mapbox聚合图,click和mousemove获取到组成此聚合点要素信息 #38

Open gangxuexue opened 4 years ago

gangxuexue commented 4 years ago

问题:如题,mapbox的聚合demo是用三层layer合起来的,如果我添加点击事件,点击聚合点,如何获取其中的组成的聚合点其中各个要素信息? image 我要知道其中7个要素信息,如果过滤出来

goldenlimit commented 4 years ago

hello @gangxuexue 我是來自iOS频道的佈道助手,看到你問到這個問題,试着先帮你解答下。 你可以嘗試用一下方法來獲取feature elements

 // Change the cursor style as a UI indicator.
            map.getCanvas().style.cursor = 'pointer';
            var feat = features[0]

            //LOOKUP THE HOVERED CLUSTER FEATURES UNDERLYING DATA POINTS
            var cluster_id = features[0].properties.cluster_id;
            var all_features = cluster.getLeaves(cluster_id, Math.floor(map.getZoom()), limit = Infinity);

            //DISPLAY THE HOVERED FEATURES IN THE POPUP
            var features_to_display = []
            all_features.forEach(function(f) {
                features_to_display.push(f.properties["Primary ID"])
            })
            // Populate the popup and set its coordinates
            // based on the feature found.

搜索到一個相關的issue 關於features in cluster,里面有一个哥们回复的一个demo你可以参考下 https://github.com/mapbox/mapbox-gl-js/issues/3318#issuecomment-331566101

他的这个Get features under a cluster应该是token过期了, 建议你根据现在有的cluster demo 然后参考我上面的那段代码就应该能access到features

希望对你有所帮助