[GIS] Extracting attributes from clustered KML layer

kmlopenlayers

Creating the vector layer from a kml file, as:

kmlLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                url: "kml_sample.kml",
                format: new ol.format.KML({
                    showPointNames: false,
                    extractStyles: false,
                    extractAttributes: true
                })
            })
        });

I can extract the attributes and display them in a popup:

map.on('click', function(evt) {
    var pixel = evt.pixel;

    var features = [];
    map.forEachFeatureAtPixel(pixel, function(feature) {
        features.push(feature);
    });
    if (features.length > 0) {
        var coordinate = features[0].getGeometry().getFirstCoordinate();

        if (features[0].get("features") && features[0].get("features").length > 1) {
            map.getView().setZoom(map.getView().getZoom()+1);
        } else { // single feature
            popupContent.innerHTML = '<p>' + features[0].get("description") + '</p>';
            popupOverlay.setPosition(coordinate);
        }
    } else {
        popupCloser.onclick();
    }
});

However, when using a Cluster:

kmlLayer = new ol.layer.Vector({
            source: new ol.source.Cluster({
                distance: 10,
                source: new ol.source.Vector({
                    url: "kml_sample.kml",
                    format: new ol.format.KML({
                        showPointNames:    false,
                        extractStyles:     false,
                        extractAttributes: true
                    })
                })
            })
        });

The attributes return undefined.

Best Answer

Apparently, when the KML vector layer is used in combination with a Cluster, the location of the fields vary:

KML without clustering:

popupContent.innerHTML = '<p>' + features[0].get("description") + '</p>';

KML with clustering:

popupContent.innerHTML = '<p>' + features[0].get("features")[0].get("description") + '</p>';