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.
1 Answer 1
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.