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
. I think openlayers stores cluster data as an array of features rather than multiple individual features? 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'));
});
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.