📄 map.js
字号:
this.maxExtent = new OpenLayers.Bounds(-180, -90, 180, 90); this.theme = OpenLayers._getScriptLocation() + 'theme/default/style.css'; // now add the options declared by the user // (these will override defaults) OpenLayers.Util.extend(this, options); }, /** * APIMethod: getTileSize * Get the tile size for the map * * Returns: * {<OpenLayers.Size>} */ getTileSize: function() { return this.tileSize; }, /********************************************************/ /* */ /* Layer Functions */ /* */ /* The following functions deal with adding and */ /* removing Layers to and from the Map */ /* */ /********************************************************/ /** * APIMethod: getLayer * Get a layer based on its id * * Parameter: * id - {String} A layer id * * Returns: * {<OpenLayers.Layer>} The Layer with the corresponding id from the map's * layer collection, or null if not found. */ getLayer: function(id) { var foundLayer = null; for (var i = 0; i < this.layers.length; i++) { var layer = this.layers[i]; if (layer.id == id) { foundLayer = layer; } } return foundLayer; }, /** * Method: setLayerZIndex * * Parameters: * layer - {<OpenLayers.Layer>} * zIdx - {int} */ setLayerZIndex: function (layer, zIdx) { layer.setZIndex( this.Z_INDEX_BASE[layer.isBaseLayer ? 'BaseLayer' : 'Overlay'] + zIdx * 5 ); }, /** * APIMethod: addLayer * * Parameters: * layer - {<OpenLayers.Layer>} */ addLayer: function (layer) { for(var i=0; i < this.layers.length; i++) { if (this.layers[i] == layer) { var msg = "You tried to add the layer: " + layer.name + " to the map, but it has already been added"; OpenLayers.Console.warn(msg); return false; } } layer.div.style.overflow = ""; this.setLayerZIndex(layer, this.layers.length); if (layer.isFixed) { this.viewPortDiv.appendChild(layer.div); } else { this.layerContainerDiv.appendChild(layer.div); } this.layers.push(layer); layer.setMap(this); if (layer.isBaseLayer) { if (this.baseLayer == null) { // set the first baselaye we add as the baselayer this.setBaseLayer(layer); } else { layer.setVisibility(false); } } else { layer.redraw(); } this.events.triggerEvent("addlayer"); }, /** * APIMethod: addLayers * * Parameters: * layers - Array({<OpenLayers.Layer>}) */ addLayers: function (layers) { for (var i = 0; i < layers.length; i++) { this.addLayer(layers[i]); } }, /** * APIMethod: removeLayer * Removes a layer from the map by removing its visual element (the * layer.div property), then removing it from the map's internal list * of layers, setting the layer's map property to null. * * a "removelayer" event is triggered. * * very worthy of mention is that simply removing a layer from a map * will not cause the removal of any popups which may have been created * by the layer. this is due to the fact that it was decided at some * point that popups would not belong to layers. thus there is no way * for us to know here to which layer the popup belongs. * * A simple solution to this is simply to call destroy() on the layer. * the default OpenLayers.Layer class's destroy() function * automatically takes care to remove itself from whatever map it has * been attached to. * * The correct solution is for the layer itself to register an * event-handler on "removelayer" and when it is called, if it * recognizes itself as the layer being removed, then it cycles through * its own personal list of popups, removing them from the map. * * Parameters: * layer - {<OpenLayers.Layer>} * setNewBaseLayer - {Boolean} Default is true */ removeLayer: function(layer, setNewBaseLayer) { if (setNewBaseLayer == null) { setNewBaseLayer = true; } if (layer.isFixed) { this.viewPortDiv.removeChild(layer.div); } else { this.layerContainerDiv.removeChild(layer.div); } OpenLayers.Util.removeItem(this.layers, layer); layer.removeMap(this); layer.map = null; // if we removed the base layer, need to set a new one if (setNewBaseLayer && (this.baseLayer == layer)) { this.baseLayer = null; for(i=0; i < this.layers.length; i++) { var iLayer = this.layers[i]; if (iLayer.isBaseLayer) { this.setBaseLayer(iLayer); break; } } } this.events.triggerEvent("removelayer"); }, /** * APIMethod: getNumLayers * * Returns: * {Int} The number of layers attached to the map. */ getNumLayers: function () { return this.layers.length; }, /** * APIMethod: getLayerIndex * * Parameters: * layer - {<OpenLayers.Layer>} * * Returns: * {Integer} The current (zero-based) index of the given layer in the map's * layer stack. Returns -1 if the layer isn't on the map. */ getLayerIndex: function (layer) { return OpenLayers.Util.indexOf(this.layers, layer); }, /** * APIMethod: setLayerIndex * Move the given layer to the specified (zero-based) index in the layer * list, changing its z-index in the map display. Use * map.getLayerIndex() to find out the current index of a layer. Note * that this cannot (or at least should not) be effectively used to * raise base layers above overlays. * * Parameters: * layer - {<OpenLayers.Layer>} * idx - {int} */ 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; i < this.layers.length; i++) { this.setLayerZIndex(this.layers[i], i); } this.events.triggerEvent("changelayer"); } }, /** * APIMethod: raiseLayer * Change the index of the given layer by delta. If delta is positive, * the layer is moved up the map's layer stack; if delta is negative, * the layer is moved down. Again, note that this cannot (or at least * should not) be effectively used to raise base layers above overlays. * * Paremeters: * layer - {<OpenLayers.Layer>} * idx - {int} */ raiseLayer: function (layer, delta) { var idx = this.getLayerIndex(layer) + delta; this.setLayerIndex(layer, idx); }, /** * APIMethod: setBaseLayer * Allows user to specify one of the currently-loaded layers as the Map's * new base layer. * * Parameters: * newBaseLayer - {<OpenLayers.Layer>} */ setBaseLayer: function(newBaseLayer) { var oldExtent = null; if(this.baseLayer) { oldExtent = this.baseLayer.getExtent(); } if (newBaseLayer != this.baseLayer) { // is newBaseLayer an already loaded layer?m if (OpenLayers.Util.indexOf(this.layers, newBaseLayer) != -1) { // make the old base layer invisible if (this.baseLayer != null) { this.baseLayer.setVisibility(false); } // set new baselayer and make it visible this.baseLayer = newBaseLayer; // Increment viewRequestID since the baseLayer is // changing. This is used by tiles to check if they should // draw themselves. this.viewRequestID++; this.baseLayer.visibility = true; //redraw all layers var center = this.getCenter(); if (center != null) { if (oldExtent == null) { // simply set center but force zoom change this.setCenter(center, this.getZoom(), false, true); } else { // zoom to oldExtent *and* force zoom change this.setCenter(oldExtent.getCenterLonLat(), this.getZoomForExtent(oldExtent, true), false, true); } } this.events.triggerEvent("changebaselayer"); } } }, /********************************************************/ /* */ /* Control Functions */ /* */ /* The following functions deal with adding and */ /* removing Controls to and from the Map */ /* */ /********************************************************/ /** * APIMethod: addControl * * Parameters: * control - {<OpenLayers.Control>} * px - {<OpenLayers.Pixel>} */ addControl: function (control, px) { this.controls.push(control); this.addControlToMap(control, px); }, /** * Method: addControlToMap * * Parameters: * * control - {<OpenLayers.Control>} * px - {<OpenLayers.Pixel>} */ addControlToMap: function (control, px) { // If a control doesn't have a div at this point, it belongs in the // viewport. control.outsideViewport = (control.div != null); control.setMap(this); var div = control.draw(px); if (div) { if(!control.outsideViewport) { div.style.zIndex = this.Z_INDEX_BASE['Control'] + this.controls.length; this.viewPortDiv.appendChild( div ); } } }, /** * APIMethod: getControl * * Parameters: * id - {String} ID of the control to return. * * Returns: * {<OpenLayers.Control>} The control from the map's list of controls * which has a matching 'id'. If none found, * returns null. */ getControl: function (id) { var returnControl = null; for(var i=0; i < this.controls.length; i++) { var control = this.controls[i]; if (control.id == id) { returnControl = control; break; } } return returnControl; }, /** * APIMethod: removeControl * Remove a control from the map. Removes the control both from the map * object's internal array of controls, as well as from the map's * viewPort (assuming the control was not added outsideViewport) * * Parameters: * control - {<OpenLayers.Control>} The control to remove. */ removeControl: function (control) { //make sure control is non-null and actually part of our map if ( (control) && (control == this.getControl(control.id)) ) { if (!control.outsideViewport) { this.viewPortDiv.removeChild(control.div) } OpenLayers.Util.removeItem(this.controls, control); } }, /********************************************************/ /* */ /* Popup Functions */ /* */ /* The following functions deal with adding and */ /* removing Popups to and from the Map */ /* */ /********************************************************/ /** * APIMethod: addPopup * * Parameters: * popup - {<OpenLayers.Popup>} * exclusive - {Boolean} If true, closes all other popups first */ addPopup: function(popup, exclusive) { if (exclusive) { //remove all other popups from screen for(var i=0; i < this.popups.length; i++) { this.removePopup(this.popups[i]); } } popup.map = this; this.popups.push(popup); var popupDiv = popup.draw(); if (popupDiv) { popupDiv.style.zIndex = this.Z_INDEX_BASE['Popup'] + this.popups.length; this.layerContainerDiv.appendChild(popupDiv); } }, /** * APIMethod: removePopup * * Parameters: * popup - {<OpenLayers.Popup>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -