[英]How to get GeoJSON properties to appear in openlayers 3 popup when using CLUSTERS?
I've built a relatively simple openlayers map and I'm attempting to get my geoJSON properties to appear in my on-click popup but no matter what I try, it either returns the geometry name object Object
or Undefined
. 我已经构建了一个相对简单的openlayers地图,并且试图将我的geoJSON属性显示在单击弹出窗口中,但是无论我尝试什么,它都将返回几何名称
object Object
或Undefined
。 I think openlayers stores cluster data as an array of features rather than multiple individual features? 我认为openlayers将群集数据存储为一组功能,而不是多个单独的功能? Here is the code for my on-click function:
这是单击功能的代码:
var
container = document.getElementById('popup'),
content_element = document.getElementById('popup-content'),
closer = document.getElementById('popup-closer');
closer.onclick = function() {
overlay.setPosition(undefined);
closer.blur();
return false;
};
var overlay = new ol.Overlay({
element: container,
positioning: 'bottom-center',
stopEvent: false
});
map.addOverlay(overlay);
// display popup on click
map.on('click', function(evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature, layer) {
return feature;
});
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
var content =coord;
//------------------------------------------------------------------------------
content += '<h5>' + feature.get('MMSI') + '</h5>';
//--------^^^^^^^ This does not work for clusters ^^^^^^------------------------
//------------------------------------------------------------------------------
content_element.innerHTML = content;
overlay.setPosition(coord);
} else {
}
I don't need the properties of the actual clusters but I do need the properties of a single point(once zoomed in and the clusters have separated into individual points). 我不需要实际聚类的属性,但确实需要单个点的属性(一旦放大,并且聚类已分离为单个点)。 Any help is much appreciated!
任何帮助深表感谢!
Try this way: 尝试这种方式:
map.on('click', function(evt) {
var ft = map.forEachFeatureAtPixel(evt.pixel, function(f, l){return f;});
if (ft) {
var features = ft.get('features'); //this is from the official examples
features.forEach(function(feature){
//assumption there is a 'name' property
console.info(feature.get('name'));
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.