[OpenLayers-Users] BBOX strategy with GeoJSON points
Puneet Kishor
punk.kish at gmail.com
Sat Mar 17 16:23:26 EDT 2012
Thanks Arnd, this really helps. Perhaps extending the .Format.GeoJSON() object to allow providing a custom read function might be worthwhile.
On Mar 17, 2012, at 2:18 PM, Arnd Wippermann wrote:
> Hi Puneet,
>
> You can overwrite the read function of the format.
> Then you should be able to grab the geojson from your response.
>
>
> //function read from OpenLayers-2.11\lib\OpenLayers\Format\GeoJSON.js
> function myReadFunction(json, type, filter)
> {
> type = (type) ? type : "FeatureCollection";
> var results = null;
>
> //instead
> /*
> if (typeof json == "string") {
> obj = OpenLayers.Format.JSON.prototype.read.apply(this,
> [json, filter]);
> } else {
> obj = json;
> }
> */
>
> //try this
> var obj = eval('(' + json + ')').result.geojson;
>
> //above no changes
> if(!obj) {
> OpenLayers.Console.error("Bad JSON: " + json);
> } else if(typeof(obj.type) != "string") {
> OpenLayers.Console.error("Bad GeoJSON - no type: " + json);
> }
> else if(this.isValidType(obj, type)) {
> ...
> ...
> break;
> }
> }
> return results;
> }
>
> var vec = new OpenLayers.Layer.Vector(
> "My Vector Layer", {
> strategies: [new OpenLayers.Strategy.BBOX()],
> protocol: new OpenLayers.Protocol.HTTP({
> url: "some_features.geojson",
> params: {
> "a": "value of a",
> "b": function() {
> return a_value_for_b;
> }
> format: new OpenLayers.Format.GeoJSON({read:myReadFunction})
> })
> }
> );
>
> map.addLayer(vec);
>
> Arnd
>
More information about the Users
mailing list