making a vector layer with geoJson in openlayer
I used to mak开发者_C百科e GML vector like this:
layer = new OpenLayers.Layer.GML("based",
"./parser2.php",
{
isBaseLayer: true,
format: OpenLayers.Format.GeoJSON,
styleMap: new OpenLayers.StyleMap(mystyle),
units:"m",
maxResolution: 0.2,
minResolution: 0.01
})
map.addLayer(layer);
but is now depreciated an for multiple raison i need to use OpenLayers.Layer.Vector but i can't succed to read a geoJon file. I tried like this:
var test = new OpenLayers.Layer.Vector("test", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
isBaseLayer: true,
url: "data.json",
styleMap: myStyles,
format: new OpenLayers.Format.JSON()
})
});
map.addLayer(test);
but unfortunately it's not working.
do you have any clue?
Thanks
I use the steps described in the following web page to add GeoJSON format data to a layer: http://thinkwhere.wordpress.com/2011/06/26/geocommons-geojson-in-openlayers/
As your GeoJSON is already correct format do not add {"type": "FeatureCollection", "features": ...}
around the GeoJSON string, as shown in this example.
In plain English, the steps are:
- Create a new
OpenLayers.Layer.Vector
layer without options to read data. - You read the URL yourself.
- In the callback function called when reading is complete you create a
OpenLayers.Format.GeoJSON()
object, use it to read features from the GeoJSON string, then add the features to the layer.
精彩评论