2

我曾经像这样制作 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);

但不幸的是它不起作用。

你有什么线索吗?

谢谢

4

1 回答 1

2

我使用以下网页中描述的步骤将 GeoJSON 格式数据添加到图层:http ://thinkwhere.wordpress.com/2011/06/26/geocommons-geojson-in-openlayers/ 因为您的 GeoJSON 格式已经正确不要{"type": "FeatureCollection", "features": ...}在 GeoJSON 字符串周围添加,如本例所示。

用简单的英语,步骤是:

  1. 创建一个OpenLayers.Layer.Vector没有读取数据选项的新图层。
  2. 您自己阅读 URL。
  3. 在读取完成时调用的回调函数中,您创建一个OpenLayers.Format.GeoJSON()对象,使用它从 GeoJSON 字符串中读取要素,然后将要素添加到图层。
于 2011-09-03T07:48:18.827 回答