在openlayer中使用geoJson制作矢量图层
我曾经像这样制作 GML 矢量:
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);
但现在已贬值,因为我需要使用 OpenLayers.Layer.Vector 但我无法成功读取 geoJon 文件。 我尝试过这样的:
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);
但不幸的是它不起作用。
你有什么线索吗?
谢谢
I used to make 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用以下网页中描述的步骤将 GeoJSON 格式数据添加到图层: http://thinkwhere.wordpress.com/2011/06/26/geocommons-geojson-in-openlayers/
由于您的 GeoJSON 格式已经正确,因此请勿在 GeoJSON 字符串周围添加
{"type": "FeatureCollection", "features": ...}
,如此示例所示。用简单的英语来说,步骤是:
OpenLayers.Layer.Vector
图层,不带读取数据的选项。OpenLayers.Format.GeoJSON()
对象,使用它从 GeoJSON 字符串中读取要素,然后将要素添加到图层中。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:
OpenLayers.Layer.Vector
layer without options to read data.OpenLayers.Format.GeoJSON()
object, use it to read features from the GeoJSON string, then add the features to the layer.