0

I have an OpenLayers vector layer that loads data form service. But it loads all data initially.

        var mylayer = new OpenLayers.Layer.Vector(null, {
            "protocol": myWFSProtocol,
            "styleMap": style,
            "strategies": [
                new OpenLayers.Strategy.BBOX(),
                new OpenLayers.Strategy.Cluster()
            ]
            "filter": new OpenLayers.Filter.Logical({
                type: OpenLayers.Filter.Logical.OR,
                filters: [
                    new OpenLayers.Filter.Comparison({
                        "type": OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
                        "property": attributeNames.catid,
                        "value": "null"
                    })
                ]
            })
        });

My filter says that get data if catid is not null, so so many data is loading at application initialized.

I want do not want to initialize it at first. I want to get data on button click. Is there any property to initializing Layer or protocol.

1 Answer 1

1

There aren't any methods to bind feature requests to a button click. You should bind the whole layer construction to the button, if you wouldn't like to request the features on initialization. You should also check if the layer has been initialized in order to avoid memory leaks.

var mylayer;
button.onclick = function() {
    if (!(mylayer instanceof OpenLayers.Layer.Vector)) {
        mylayer = new OpenLayers.Layer.Vector(null, {
            "protocol": myWFSProtocol,
            "styleMap": style,
            "strategies": [
                new OpenLayers.Strategy.BBOX(),
                new OpenLayers.Strategy.Cluster()
            ]
            "filter": new OpenLayers.Filter.Logical({
                type: OpenLayers.Filter.Logical.OR,
                filters: [
                    new OpenLayers.Filter.Comparison({
                        "type": OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
                        "property": attributeNames.catid,
                        "value": "null"
                    })
                ]
            })
        });
        map.addLayer(mylayer);
    }
};

If I misinterpreted you question, and you would like to initialize the layer with a filter, which does not fetch any data, and change it on the run, then you can do it by creating a dummy filter, changing it with the button's onclick event, and refreshing the layer if needed:

var mylayer = new OpenLayers.Layer.Vector(null, {
    "protocol": myWFSProtocol,
    "styleMap": style,
    "strategies": [
        new OpenLayers.Strategy.BBOX(),
        new OpenLayers.Strategy.Cluster()
    ]
    "filter": new OpenLayers.Filter.Comparison({
        "type": OpenLayers.Filter.Logical.EQUAL_TO,
        "property": "theServerProbablyWontHaveFeaturesWithThisPropertyName",
        "value": "1"
    })
});

button.onclick = function() {
    mylayer.filter = new OpenLayers.Filter.Logical({
        type: OpenLayers.Filter.Logical.OR,
        filters: [
            new OpenLayers.Filter.Comparison({
                "type": OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
                "property": attributeNames.catid,
                "value": "null"
            })
        ]
    })
};
mylayer.refresh();

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.