—————————————————–
OpenLayers.Class.create();//该函数用于讲类初始化,相当于类的构造函数,它调用了类的initial()方法
OpenLayers.Layer.GML.initial();//类初始化
// OpenLayers.Layer.GML继承自 OpenLayers.Layer.Vector,也调用OpenLayers.Layer.Vector
的构造函数
OpenLayers.Layer.Vector.prototype.initialize.apply(this, newArguments);
// OpenLayers.Layer.Vector继承自 OpenLayers.Layer,也调用OpenLayers.Layer的构造函数
OpenLayers.Layer.prototype.initialize.apply(this, arguments);
//OpenLayers.Layer初始化时候会给图层创建id,并创建一个div容器用于装载该图层
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + “_”);
this.div = OpenLayers.Util.createDiv();
this.div = OpenLayers.Util.createDiv();
this.div.style.width = “100%”;
this.div.style.height = “100%”;
this.div.id = this.id;
//并给该div容器绑定事件 EVENT_TYPES: [ “loadstart”, “loadend”, “loadcancel”],OpenLayers.Layer初始化完毕
this.events = new OpenLayers.Events(this, this.div, this.EVENT_TYPES);
//然后回到 OpenLayers.Layer.Vector的初始化中,调用下面的函数通过判断浏览器种类来决定用
那种渲染器
this.assignRenderer();
//回到gml初始化中
//初始化完毕【最后返回到OpenLayers.Class.create()】
///////////////////////////
//调用map.addLayer(layer)
//设置添加的图层的显示顺序
this.setLayerZIndex(layer, this.layers.length);
//有一个叫做 this.layerContainerDiv 的div用来装载所有的图层,这里将把装载该图层的div添加到this.layerContainerDiv 中
this.layerContainerDiv.appendChild(layer.div);
//并将改图层添加到map的layers属性中
this.layers.push(layer);
//设置layer的map属性,让map和layer引用彼此【这是IE引起内存泄露的主要因素之一,不过openlayers
有一套自己的垃圾回收机制控制了这一点】
layer.setMap(this);
//假如该图isBaseLayer,那么将map的 底图设置为该图层
this.setBaseLayer(layer);
//假如不是baselayer那么就作为一般图层对待,将该图层显示在指定的窗口extent,如果没有设置
map的extent属性,地图是显示不出来的
//map.zoomToExtent(new OpenLayers.Bounds(114.24,30.56,114.27,30.59));或者
setCenter()均可以指定extent
layer.moveTo(this.getExtent(), true);
//layer.moveTo()先调用 OpenLayers.Layer.Vector的moveto方法,设定渲染器的显示范围
OpenLayers.Layer.Vector.prototype.moveTo.apply(this, arguments);
//数据还没有加载则layer.moveTo()开始加载数据
this.loadGML();
//this.loadGML()加载数据,并绑定相应的结果处理函数
OpenLayers.loadURL(this.url, null, this, this.requestSuccess, this.requestFailure);
//map.addLayer(layer)中触发 “addlayer” 事件
this.events.triggerEvent(”addlayer”);
///////////////////////////
//OpenLayers.loadURL(this.url, null, this, this.requestSuccess, this.requestFailure);开始加载数据
//假如加载成功则则将结果然会给this.requestSuccess进行处理
//this.requestSuccess(response)解析返回的gml数据
requestSuccess
//用OpenLayers.Format.GML 的read()函数来解析gml数据,解析结果为Geometry对象的集合, this.addFeatures用户将这些地理对象显示出来
var gml = this.format ? new this.format() : new OpenLayers.Format.GML();
this.addFeatures(gml.read(doc));
//////////##########read#####OpenLayers.Format.GML()########///////////////
#region GML文件解析段
#end region
//////////##########addFeatures#####OpenLayers.Layer.Vector(),即OpenLayers.Format.GML的父类########///////////////
//addFeatures中调用drawFeature来绘制单个地理对象
this.drawFeature(feature);
\\\\\\\\renderer段\\\\\\\\\\\/////////
//OpenLayers.Layer.Vector的drawFeature又用来绘制图形,传入的参数是地理对象和绘图样式
this.renderer.drawFeature(feature, style);
//renderer调用自己的drawGeometry方法来绘图
this.drawGeometry(feature.geometry, style, feature.id);
//renderer的drawGeometry函数先判断传入的feature的类型,通过getNodeType()得到
需要创建的vml节点的类型【假定目前renderer为IE】,
//再通过nodeFactory创建vml节点,把改节点加入到root中,root为一个group元素,再调用drawGeometryNode改变vml节点的坐标属性
if ((geometry.CLASS_NAME == “OpenLayers.Geometry.MultiPoint”) ||
(geometry.CLASS_NAME == “OpenLayers.Geometry.MultiLineString”) ||
(geometry.CLASS_NAME == “OpenLayers.Geometry.MultiPolygon”)) {
for (var i = 0; i < geometry.components.length; i++) {
this.drawGeometry(geometry.components[i], style, featureId);
}
return;
};
var nodeType = this.getNodeType(geometry);
var node = this.nodeFactory(geometry.id, nodeType, geometry);
node._featureId = featureId;
node._geometryClass = geometry.CLASS_NAME;
node._style = style;
this.root.appendChild(node);
//now actually draw the node, and style it
this.drawGeometryNode(node, geometry);
附:在renderer初始化的时候OpenLayers.Layer.Elements会创建
this.rendererRoot = this.createRenderRoot();
this.root = this.createRoot();
其中,rendererRoot为一个div,用于装载所有的VML元素,而root为一个group,用于装载目前这个
图层的vml元素,root为rendererRoot的一级子节点
//drawGeometryNode根据传入地理对象的geometry.CLASS_NAME来判断调用点线还是面的绘图
函数还对地理对象进行绘制
this.drawGeometryNode(node, geometry);