OpenLayers中的Protocol协议问题

最近用OpenLayers 在两处见到用OpenLayers.Protocol.WFS的地方 。

一个是矢量图层请求WFS服务如

1。wfs = new OpenLayers.Layer.Vector("Editable Features", {
  strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy],
  projection: new OpenLayers.Projection("EPSG:4326"),
  styleMap: styles,
  protocol: new OpenLayers.Protocol.WFS({
  version: "1.1.0",
  srsName: "EPSG:4326",
  url: "http://demo.opengeo.org/geoserver/wfs",
  featureNS : "http://opengeo.org",
  featureType: "roads",
  geometryName: "the_geom",
  schema: "http://demo.opengeo.org/geoserver/wfs/DescribeFeatureType?version=1.1.0&typename=og:roads"
  })
  }); 

其中 new OpenLayers.protocol.wfs()里面的参数是URL用于请求WFS服务的地址,大家都知道 WFS服务是以GML数据形式返回的

featureNS featureType geometryName这几个参数是解析GML数据时候使用的 如果不提供这几个参数 ,我们还是能够获取到GML数据,

但是即使我们获取到GML数据,如果不解析成feature,那么这个矢量图层也没有任何东西。

2。另一个就是getfeature控件向服务器发送请求获取要素服务如下:

xcontrol.getselected=new OpenLayers.Control.GetFeature({
protocol: OpenLayers.Protocol.WFS.fromWMSLayer(highlayer),
box: false,
hover: false
});
xcontrol.getselected.setMap(map);
xcontrol.getselected.events.register("featureselected", this, function(e) {
highlightLayer.removeAllFeatures();
var feature=e.feature;
feature.fid=feature.geometry.toString();
feature.data=feature.attributes.sheet_num;
highlightLayer.addFeatures([feature]);
Onselectfeature(feature);
});

这个是我在项目中用到的一个控件,适用于从WMS图层中获取Feature时候使用的,其中有一个问题是xcontrol.getselected.setMap(map),起初我在文件中引用的时候是不暴错的 ,当我把控件定义的位置放到别的JS文件里的时候就会报一个

TypeError: this.map is null
 
this.map.events.registerPriority(name, this, method);

这样的错误,其实我也不知道为什么会报这个错误,但我肯定是当我激活这个控件的时候报的错误,于是我就跟踪代码发现在this.handlers[i].activate();在到handler.js文件中发现,activate()函数调用了这个方法

register: function (name, method) {
// TODO: deal with registerPriority in 3.0
this.map.events.registerPriority(name, this, method);
this.map.events.registerPriority(name, this, this.setEvent);
},

其中的this.map不久是刚才报的错误吗,然后我有发现

setMap: function (map) {
this.map = map;
},

是给this.map赋值的 所以我就加了xcontrol.getselected.setMap(map)这句话。终于解决问题了。

posted on 2012-12-05 14:35  Longlone  阅读(726)  评论(0编辑  收藏  举报

导航