4

I have one KML layer that has roughly 900 features and I added OpenLayer.Cluster.Strategy to help the layer display a little quicker.

My KML displays the attributes when clicked on before clustering was enabled, but now the bubble displays 'undefined, undefined'.

How did the clustering change the extractAttributes property?

            var state = new OpenLayers.Layer.Vector("State Probation", {
            projection: map.displayProjection,
            strategies: [new OpenLayers.Strategy.Fixed(), new OpenLayers.Strategy.Cluster()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "state.kml",
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true
                })
            })
        });


        select = new OpenLayers.Control.SelectFeature([sample, state],
            {
                clickout: true, toggle: false,
                multiple: true, hover: false,
                toggleKey: "ctrlKey", // ctrl key removes from selection
                multipleKey: "shiftKey" // shift key adds to selection
            });

        sample.events.on({
            "featureselected": onFeatureSelect,
            "featureunselected": onFeatureUnselect
        });

        state.events.on({
            "featureselected": onFeatureSelect,
            "featureunselected": onFeatureUnselect
        });

        map.addControl(select);
        select.activate();   

        map.addControl(new OpenLayers.Control.LayerSwitcher());



    function onPopupClose(evt) {
        select.unselectAll();
    }

    function onFeatureSelect(event) {
        var feature = event.feature;
        var selectedFeature = feature;
        var popup = new OpenLayers.Popup.FramedCloud("chicken", 
            feature.geometry.getBounds().getCenterLonLat(),
            new OpenLayers.Size(100,100),
            "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description,
            null, true, onPopupClose
        );
        feature.popup = popup;
        map.addPopup(popup);
    }
    function onFeatureUnselect(event) {
        var feature = event.feature;
        if(feature.popup) {
            map.removePopup(feature.popup);
            feature.popup.destroy();
            delete feature.popup;
        }
    }
1
  • ![enter image description here](i.stack.imgur.com/xGc2R.png) Marker may refer to: Marker pen, a felt-tipped pen used for drawing and/or coloring Marker (linguistics), a morpheme that indicates some grammatical function Marker (telecommunications), a special-purpose computer Marker, a set of sewing patterns placed over cloth to be cut Marker or Clapperboard, equipment used during filming Paintball marker or paintball gun, an air gun Biological marker, or biomarker, a substance used as an indicator of a biological state Genetic marker, a DNA sequenc
    – rakesh
    May 30, 2017 at 12:52

2 Answers 2

8

When working with clusters, OpenLayers creates an array of attributes to store the attributes of all features in the cluster. When your points are in a cluster you need to access the array instead of the attribute directly. Example (untested):

function onFeatureSelect(event) {
    var feature = event.feature;
    var selectedFeature = feature;
    var text = ""
    if(feature.cluster) {
        for(var i=0; i<feature.cluster.length; i++) {
            text += "<h2>"+feature.cluster[i].attributes.name + "</h2>" + 
                   feature.cluster[i].attributes.description + "\n";
        }
    } else {
        text = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description;
    }

    var popup = new OpenLayers.Popup.FramedCloud("chicken", 
        feature.geometry.getBounds().getCenterLonLat(),
        new OpenLayers.Size(100,100),
        text,
        null, true, onPopupClose
    );
    feature.popup = popup;
    map.addPopup(popup);
}
0
0

this works for me:

function onFeatureSelect(event) {
    var feature = event.feature;
    var layerName = event.feature.layer.name;
    var content = "unInitialized";
    // alert ('selected this feature='+feature.id+ '<br/>Atributos: '+feature.attributes.description);
    if ( (feature.attributes.name > '') || (feature.attributes.description > '') ) {
        content = feature.attributes.name + '<br/>'+feature.attributes.description;
    } else {
        if (feature.cluster) {
            content = feature.attributes.count + ' ' + layerName ;
            for (var i = 0; i < feature.attributes.count; i++) {
                    var feat = feature.cluster[i];
                    content += '<br/>'+feat.attributes.name;
            }
        } else {
            content = 'NOT a cluster either';
        }
    }

    popup = new OpenLayers.Popup.FramedCloud("chicken", 
                             feature.geometry.getBounds().getCenterLonLat(),
                             new OpenLayers.Size(100,100),
                             content,
                             null, true, onFeatureUnselect);
    popup.setBackgroundColor('white');
    feature.popup = popup;
    map.addPopup(popup);
    // GLOBAL variable, in case popup is destroyed by clicking CLOSE box
    lastfeature = feature;
    // alert ('select'+lastfeature.name);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.