OpenLayers学习11-格式Format控件Control Posted on 2015-02-08 | In OpenLayers学习 | Format,没太看懂, 应该只是个父类.先看个大概齐, 回头看了别的类 再返回头来再补充吧 Control类只定义了一堆接口, 如初始化, GC, 使能等等.等看到他的子类估计才能用得上这些 一:Format12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667//Format感觉就是个父类, 下边的子类都使用他定义的接口 OpenLayers.Format = OpenLayers.Class({ //构造需要的选项 options: null, //对象持有一个OpenLayers.Projection, 使用 reads 或者 writes 接口 externalProjection: null, /** * APIProperty: internalProjection * {} When passed a externalProjection and * internalProjection, the format will reproject the geometries it * reads or writes. The internalProjection is the projection used by * the geometries which are returned by read or which are passed into * write. In order to reproject, a projection transformation function * for the specified projections must be available. This support may be * provided via proj4js or via a custom transformation function. See * {} for more information on * custom transformations. */ internalProjection: null, //根据keepData配置, 存储data data: null, //保持最新的 read data的选项 keepData: false, //扩展本对象, 更新选项 initialize: function(options) { OpenLayers.Util.extend(this, options); this.options = options; }, //GC的接口, 此父类为空 destroy: function() { }, //这个方法等着子类继承重写, 没继承的话直接报错 read: function(data) { throw new Error('Read not implemented.'); }, //同上 write: function(object) { throw new Error('Write not implemented.'); }, CLASS_NAME: "OpenLayers.Format"}); //这个完全不知所云, 貌似为了拿到一个默认的Format父类OpenLayers.Format.CSWGetRecords = function(options) { options = OpenLayers.Util.applyDefaults( options, OpenLayers.Format.CSWGetRecords.DEFAULTS ); var cls = OpenLayers.Format.CSWGetRecords["v"+options.version.replace(/\./g, "_")]; if(!cls) { throw "Unsupported CSWGetRecords version: " + options.version; } return new cls(options);};OpenLayers.Format.CSWGetRecords.DEFAULTS = { "version": "2.0.2"}; 二: 控件类123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189OpenLayers.Control = OpenLayers.Class({ //分配的id id: null, //OpenLayers.Map, addControl的map对象 map: null, //control的容器, 没有的话就是map div: null, //OpenLayers.Control.Panel用的type标记 type: null, //是否允许被选择 默认不许, 因为这样不影响地图操作 比如拖拽 allowSelection: false, //控件的css类 displayClass: "", //tooltips字串 title: "", //自动激活控件, 添加到地图后, 是否自动activate autoActivate: false, //当前的激活状态 active: null, //控件handler的options对象 handlerOptions: null, //控件的handler handler: null, //会自动添加到OpenLayers.Events.on, 构造函数里要传入此对象 eventListeners: null, //给控件注册事件的 /* * (code) * control.events.register(type, obj, listener); * (end) */ events: null, //构造 var control = new OpenLayers.Control({div: myDiv}); initialize: function (options) { //缩短类名: olControl this.displayClass = this.CLASS_NAME.replace("OpenLayers.", "ol").replace(/\./g, ""); OpenLayers.Util.extend(this, options);//合并options this.events = new OpenLayers.Events(this);//自身持有new OpenLayers.Events(this)出来的事件对象 if(this.eventListeners instanceof Object) { this.events.on(this.eventListeners);//如果传参里有eventListeners, 就用刚new出来的this.events对象on注册 } //不指定id就olControl_自增id if (this.id == null) { this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); } }, //GC destroy: function () { //先gc事件, 再gc事件eventListeners if(this.events) { if(this.eventListeners) { this.events.un(this.eventListeners); } this.events.destroy(); this.events = null; } this.eventListeners = null; //gc持有的handler if (this.handler) { this.handler.destroy(); this.handler = null; } if(this.handlers) { for(var key in this.handlers) { if(this.handlers.hasOwnProperty(key) && typeof this.handlers[key].destroy == "function") { this.handlers[key].destroy(); } } this.handlers = null; } //删除对map的引用 if (this.map) { this.map.removeControl(this); this.map = null; } //断开dom的引用 this.div = null; }, //持有map setMap: function(map) { this.map = map; if (this.handler) { this.handler.setMap(map); } }, //渲染到指定px draw: function (px) { //没有div的话, 生成一个 if (this.div == null) { this.div = OpenLayers.Util.createDiv(this.id); this.div.className = this.displayClass; if (!this.allowSelection) { this.div.className += " olControlNoSelect";//类名 this.div.setAttribute("unselectable", "on", 0);//最后一个参数0查半天不知道干嘛的 this.div.onselectstart = OpenLayers.Function.False; } if (this.title != "") { this.div.title = this.title; } } if (px != null) { //为了预防传参px改变或者被回收, 特意clone了一份 this.position = px.clone(); } //画 this.moveTo(this.position); return this.div; }, //用style.left/top 绘制在持有的div上 moveTo: function (px) { if ((px != null) && (this.div != null)) { this.div.style.left = px.x + "px"; this.div.style.top = px.y + "px"; } }, //激活控件 activate: function () { //已激活的直接返回 if (this.active) { return false; } //激活控件持有的handler if (this.handler) { this.handler.activate(); } this.active = true;//状态位 if(this.map) { //往viewPortDiv中加一个Active类 OpenLayers.Element.addClass( this.map.viewPortDiv, this.displayClass.replace(/ /g, "") + "Active"//去空格 ); } this.events.triggerEvent("activate");//触发activate事件 return true; }, //关闭控件使能 deactivate: function () { //已激活的 if (this.active) { if (this.handler) { this.handler.deactivate();//handler先关掉 } this.active = false;//状态 if(this.map) { //删掉Active类 OpenLayers.Element.removeClass( this.map.viewPortDiv, this.displayClass.replace(/ /g, "") + "Active" ); } this.events.triggerEvent("deactivate");//触发"deactivate"事件 return true; } return false; }, CLASS_NAME: "OpenLayers.Control"}); //三类常量 OpenLayers.Control.TYPE_BUTTON = 1;OpenLayers.Control.TYPE_TOGGLE = 2;OpenLayers.Control.TYPE_TOOL = 3;