AW: [OpenLayers-Users] BBOX strategy with GeoJSON points

Arnd Wippermann arnd.wippermann at web.de
Sat Mar 17 15:18:42 EDT 2012


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