Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a layer with a BBOX strategy with a script protocol to get the data from our server. I'd like make use of the callback function http://dev.openlayers.org/docs/files/OpenLayers/Protocol/Script-js.html#OpenLayers.Protocol.Script.callback to intercept the server response and modify it based on the current state of the map (due to user interactions) If I understand it correctly, that function should get the 'raw' response of the server. But for now it just doesn't do anything:

this.layer = new OpenLayers.Layer.Vector("POI",{
        strategies: [
            new OpenLayers.Strategy.BBOX({
                resFactor: 2,
                active: false,
                autoActivate: false,
                ratio: 1.3,
                noAbort: true
            }), 
            new OpenLayers.Strategy.Cluster({
                distance: this.clusterDistance,
                threshold: this.clusterThreshold
            })
        ],
        protocol: new OpenLayers.Protocol.Script({
            url: 'http://localhost/tests/poi',
            callback: tbPoi.myOwnCallback,
            callbackKey: 'callback',
            format: new OpenLayers.Format.GeoJSON(in_options),
            params: {
                poi: tbPoi.getPoi,
                geozone: tbPoi.getGeozone
            }
        }),
        styleMap: new OpenLayers.StyleMap({
            "default": style,
            "vertex": tbMap.vertexStyle
        }, {extendDefault: false})
    });

myOwnCallback: function(data){
    console.log(data);
},

The myOwnCallback function is completely ignored. I have no idea how to make this work. I searched for examples but couldn't find one.

I want this function to test if individual features returned by the server are also on my drawing layer (if a user is modifying one) So I don't get duplicates on the map.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

OK, I've figured this out. You can use the option: parseFeatures in the protocol:

protocol: new OpenLayers.Protocol.Script({
        url: 'http://localhost/tests/poi',
        callback: tbPoi.myOwnCallback,
        callbackKey: 'callback',
        format: new OpenLayers.Format.GeoJSON(in_options),
        parseFeatures: myOwnCallback,
        params: {
            poi: tbPoi.getPoi,
            geozone: tbPoi.getGeozone
        }
}),

The myOwnCallback function should use the formatter to give the correct return.

myOwnCallback: function(data) {
    return this.format.read(data);
},

The data it receives is the GeoJSON object as returned by the server. So in the function you can deconstruct and change it any way you like. I've made a new array with the features that are not on the drawing layer and put that back as the feature part of the data object.

data.features = temp;

works great.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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