2011-09-01 44 views
2

制作矢量层GeoJSON的我用来做GML载体是这样的:在openlayer

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); 

但很不幸的是它不工作。

你有什么线索吗?

感谢

回答

2

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

用简单的英语,步骤是:

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