在openlayer中使用geoJson创建矢量图层

时间:2011-09-01 12:56:47

标签: vector openlayers 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);

但不幸的是它没有用。

你有什么线索吗?

由于

1 个答案:

答案 0 :(得分:2)

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

用简单的英语,步骤是:

  1. 创建新的OpenLayers.Layer.Vector图层,但没有选项来读取数据。
  2. 您自己阅读了网址。
  3. 在读取完成时调用的回调函数中,创建一个OpenLayers.Format.GeoJSON()对象,用它来读取GeoJSON字符串中的要素,然后将这些要素添加到图层中。