OpenLayers源码学习15:地图Map_2 Posted on 2015-03-02 | In OpenLayers学习 | map类这回看的是”构造”和”控件”之前的函数. 包括一些工具函数和”layers”方法估计”控件”的方法也类似, 但”控件”类我还没看… 还是穿插着看吧 会比较快 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134//返回viewPortDiv的DOM元素 getViewport: function() { return this.viewPortDiv;}, // 把map渲染到指定divrender: function(div) { this.div = OpenLayers.Util.getElement(div); OpenLayers.Element.addClass(this.div, 'olMap'); this.viewPortDiv.parentNode.removeChild(this.viewPortDiv);//先删掉viewPort this.div.appendChild(this.viewPortDiv);//再添加viewPoint this.updateSize();//重绘尺寸}, // 销毁方法, 在这留一个方法是为了手动调用销毁unloadDestroy: null, //当map被销毁后, 需要停止监听updateSize方法updateSizeDestroy: null, //销毁方法 , 应该在销毁map的container之前调用destroy:function() { // 没有就是已经被销毁了 if (!this.unloadDestroy) { return false; } // 先pan动画停止, 再置null if(this.panTween) { this.panTween.stop(); this.panTween = null; } // 先把zoom停止, 再置null if(this.zoomTween) { this.zoomTween.stop(); this.zoomTween = null; } // 停止监听window的 'unload'事件 OpenLayers.Event.stopObserving(window, 'unload', this.unloadDestroy); this.unloadDestroy = null; //如果resize上有事件 停止监听 if (this.updateSizeDestroy) { OpenLayers.Event.stopObserving(window, 'resize', this.updateSizeDestroy); } this.paddingForPopups = null;//气泡间距的缓存 if (this.controls != null) { for (var i = this.controls.length - 1; i>=0; --i) { this.controls[i].destroy();//遍历置空controls } this.controls = null; } if (this.layers != null) { for (var i = this.layers.length - 1; i>=0; --i) { //遍历销毁layer, 传false是不让设置成base layer this.layers[i].destroy(false); } this.layers = null; } //删掉viewport的DOM if (this.viewPortDiv && this.viewPortDiv.parentNode) { this.viewPortDiv.parentNode.removeChild(this.viewPortDiv); } this.viewPortDiv = null;//缓存置空 if (this.tileManager) { this.tileManager.removeMap(this); this.tileManager = null; } if(this.eventListeners) { this.events.un(this.eventListeners); this.eventListeners = null; } this.events.destroy();//event的销毁 this.events = null; this.options = null;}, //更新构造里的optionssetOptions: function(options) { var updatePxExtent = this.minPx && options.restrictedExtent != this.restrictedExtent; OpenLayers.Util.extend(this, options); // 重新计算 minPx 和 maxPx updatePxExtent && this.moveTo(this.getCachedCenter(), this.zoom, { forceZoomChange: true });}, //返回tileSize getTileSize: function() { return this.tileSize; }, // 从array中查找property目标项目, match是匹配规则:match.test(map[array][i][property]) getBy: function(array, property, match) { var test = (typeof match.test == "function"); var found = OpenLayers.Array.filter(this[array], function(item) { return item[property] == match || (test && match.test(item[property])); }); return found;}, // 查找对应的layer,相当于match.test(layer[property])getLayersBy: function(property, match) { return this.getBy("layers", property, match);}, // 按图层名称查找layer, 相当于name.test(layer.name)getLayersByName: function(match) { return this.getLayersBy("name", match);}, //按 "CLASS_NAME"查找layer, 相当于type.test(layer.CLASS_NAME)getLayersByClass: function(match) { return this.getLayersBy("CLASS_NAME", match);}, // 查找对应控件 和查找图层类似, match.test(controls[property])getControlsBy: function(property, match) { return this.getBy("controls", property, match);}, // 根据"CLASS_NAME"查找控件, 类似于图层: type.test(control.CLASS_NAME)getControlsByClass: function(match) { return this.getControlsBy("CLASS_NAME", match);}, 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239/********************************************************//* *//* Layer Functions *//* *//* The following functions deal with adding and *//* removing Layers to and from the Map *//* *//********************************************************/ //用id获得layer getLayer: function(id) { var foundLayer = null;//默认null for (var i=0, len=this.layers.length; i<len; i++) { var layer = this.layers[i]; if (layer.id == id) { foundLayer = layer;//遍历this.layers数组 break; } } return foundLayer; }, //对<OpenLayers.Layer>参数对象, 设置zIdx setLayerZIndex: function (layer, zIdx) { layer.setZIndex( //1判断此layer是否是baseLayer, //2去Z_INDEX_BASE配置里去查对应的z-index值 //3把目标值*5 加上配置值, 设置 this.Z_INDEX_BASE[layer.isBaseLayer ? 'BaseLayer' : 'Overlay'] + zIdx * 5 ); }, //重新设置图层的zidx //用上个方法的公式, 实际上是自身zidx*5 再加配置值 resetLayersZIndex: function() { for (var i=0, len=this.layers.length; i<len; i++) { var layer = this.layers[i]; this.setLayerZIndex(layer, i); } }, //往map实例上添加图层 //返回true就是已添加 addLayer: function (layer) { for(var i = 0, len = this.layers.length; i < len; i++) { if (this.layers[i] == layer) {// 直接在缓存里找对应layer, 双等比较内存值 return false; } } //先触发一下"preaddlayer"事件, 失败的话直接返回 if (this.events.triggerEvent("preaddlayer", {layer: layer}) === false) { return false; } //处理allOverlays配置为true时, 新图层的isBaseLayer置为false if(this.allOverlays) { layer.isBaseLayer = false; } layer.div.className = "olLayerDiv"; layer.div.style.overflow = ""; //图层的z-index是按图层数量设定的, 队列越靠后zidex越大 this.setLayerZIndex(layer, this.layers.length); //这个layer.isFixed不知道是什么配置 if (layer.isFixed) { //在map的viewport里添加此图层的div this.viewPortDiv.appendChild(layer.div); } else { //在map的layerContainerDiv里添加 this.layerContainerDiv.appendChild(layer.div); } this.layers.push(layer);//缓存 layer.setMap(this);//这个layer方法回头再看 if (layer.isBaseLayer || (this.allOverlays && !this.baseLayer)) { if (this.baseLayer == null) { // 头一次设置baselayer this.setBaseLayer(layer); } else { // 隐藏 layer.setVisibility(false); } } else { //重绘 layer.redraw(); } //触发这两个事件 this.events.triggerEvent("addlayer", {layer: layer}); layer.events.triggerEvent("added", {map: this, layer: layer}); //回头再看这个添加 layer.afterAdd(); return true; }, //把传入的图层树组使用addLayer方法添加 addLayers: function (layers) { for (var i=0, len=layers.length; i<len; i++) { this.addLayer(layers[i]); } }, //删除图层 removeLayer: function(layer, setNewBaseLayer) { if (this.events.triggerEvent("preremovelayer", {layer: layer}) === false) { return; } //第二个参数默认为true if (setNewBaseLayer == null) { setNewBaseLayer = true; } //fixed参数, 有的话就是在viewport里添加 if (layer.isFixed) { this.viewPortDiv.removeChild(layer.div); } else { //添加到layerContainerDiv里 this.layerContainerDiv.removeChild(layer.div); } OpenLayers.Util.removeItem(this.layers, layer);//删除数组元素 layer.removeMap(this); layer.map = null;//持有map置为null // baseLayer被删掉后, if(this.baseLayer == layer) { this.baseLayer = null; if(setNewBaseLayer) { for(var i=0, len=this.layers.length; i<len; i++) { var iLayer = this.layers[i]; if (iLayer.isBaseLayer || this.allOverlays) { this.setBaseLayer(iLayer); break; } } } } //对删除的图层调整zidx this.resetLayersZIndex(); this.events.triggerEvent("removelayer", {layer: layer}); layer.events.triggerEvent("removed", {map: this, layer: layer}); }, // 获得图层的数量 getNumLayers: function () { return this.layers.length; }, // 在layers数组里查找目标图层的顺序 getLayerIndex: function (layer) { return OpenLayers.Util.indexOf(this.layers, layer); }, // 设置图层顺序 setLayerIndex: function (layer, idx) { var base = this.getLayerIndex(layer);//当前顺序 //调整上下限 if (idx < 0) { idx = 0; } else if (idx > this.layers.length) { idx = this.layers.length; } //顺序需要调整 if (base != idx) { this.layers.splice(base, 1);//从缓存里删掉原有 this.layers.splice(idx, 0, layer); for (var i=0, len=this.layers.length; i<len; i++) { this.setLayerZIndex(this.layers[i], i); } //改变图层事件 this.events.triggerEvent("changelayer", { layer: layer, property: "order" }); if(this.allOverlays) { //baseLayer的设置 if(idx === 0) { this.setBaseLayer(layer); } else if(this.baseLayer !== this.layers[0]) { this.setBaseLayer(this.layers[0]); } } } }, // 提高图层的zidx为原zidx + delta raiseLayer: function (layer, delta) { var idx = this.getLayerIndex(layer) + delta; this.setLayerIndex(layer, idx); }, // 设置baseLayer setBaseLayer: function(newBaseLayer) { if (newBaseLayer != this.baseLayer) { //确认目标图层在缓存数组中 if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) { //拿到中心点, 转换出对应resolution var center = this.getCachedCenter(); var newResolution = OpenLayers.Util.getResolutionFromScale( this.getScale(), newBaseLayer.units ); //原baseLayer要隐藏 if (this.baseLayer != null && !this.allOverlays) { this.baseLayer.setVisibility(false); } // 缓存baseLayer this.baseLayer = newBaseLayer; //考虑重绘 if(!this.allOverlays || this.baseLayer.visibility) { this.baseLayer.setVisibility(true); //没在范围内的要重绘 if (this.baseLayer.inRange === false) { this.baseLayer.redraw(); } } // 重新定位中心点 if (center != null) { // 换算新图层对应的zoom var newZoom = this.getZoomForResolution( newResolution || this.resolution, true ); // 切换中心点 this.setCenter(center, newZoom, false, true); } //触发changebaselayer事件 this.events.triggerEvent("changebaselayer", { layer: this.baseLayer }); } } },