📄 layer.js
字号:
* size - {<OpenLayers.Size>} */ setTileSize: function(size) { var tileSize = (size) ? size : ((this.tileSize) ? this.tileSize : this.map.getTileSize()); this.tileSize = tileSize; if(this.gutter) { // layers with gutters need non-null tile sizes //if(tileSize == null) { // OpenLayers.console.error("Error in layer.setMap() for " + // this.name + ": layers with " + // "gutters need non-null tile sizes"); //} this.imageOffset = new OpenLayers.Pixel(-this.gutter, -this.gutter); this.imageSize = new OpenLayers.Size(tileSize.w + (2*this.gutter), tileSize.h + (2*this.gutter)); } }, /** * APIMethod: getVisibility * * Returns: * {Boolean} The layer should be displayed (if in range). */ getVisibility: function() { return this.visibility; }, /** * APIMethod: setVisibility * Set the visibility flag for the layer and hide/show & redraw * accordingly. Fire event unless otherwise specified * * Note that visibility is no longer simply whether or not the layer's * style.display is set to "block". Now we store a 'visibility' state * property on the layer class, this allows us to remember whether or * not we *desire* for a layer to be visible. In the case where the * map's resolution is out of the layer's range, this desire may be * subverted. * * Parameters: * visible - {Boolean} Whether or not to display the layer (if in range) */ setVisibility: function(visibility) { if (visibility != this.visibility) { this.visibility = visibility; this.display(visibility); this.redraw(); if (this.map != null) { this.map.events.triggerEvent("changelayer"); } this.events.triggerEvent("visibilitychanged"); } }, /** * APIMethod: display * Hide or show the Layer * * Parameters: * display - {Boolean} */ display: function(display) { if (display != (this.div.style.display != "none")) { this.div.style.display = (display) ? "block" : "none"; } }, /** * Method: calculateInRange * * Returns: * {Boolean} The layer is displayable at the current map's current * resolution. */ calculateInRange: function() { var inRange = false; if (this.map) { var resolution = this.map.getResolution(); inRange = ( (resolution >= this.minResolution) && (resolution <= this.maxResolution) ); } return inRange; }, /** * APIMethod: setIsBaseLayer * * Parameters: * isBaseLayer - {Boolean} */ setIsBaseLayer: function(isBaseLayer) { if (isBaseLayer != this.isBaseLayer) { this.isBaseLayer = isBaseLayer; if (this.map != null) { this.map.events.triggerEvent("changelayer"); } } }, /********************************************************/ /* */ /* Baselayer Functions */ /* */ /********************************************************/ /** * Method: initResolutions * This method's responsibility is to set up the 'resolutions' array * for the layer -- this array is what the layer will use to interface * between the zoom levels of the map and the resolution display * of the layer. * * The user has several options that determine how the array is set up. * * For a detailed explanation, see the following wiki from the * openlayers.org homepage: * http://trac.openlayers.org/wiki/SettingZoomLevels */ initResolutions: function() { // These are the relevant options which are used for calculating // resolutions information. // var props = new Array( 'projection', 'units', 'scales', 'resolutions', 'maxScale', 'minScale', 'maxResolution', 'minResolution', 'minExtent', 'maxExtent', 'numZoomLevels', 'maxZoomLevel' ); // First we create a new object where we will store all of the // resolution-related properties that we find in either the layer's // 'options' array or from the map. // var confProps = {}; for(var i=0; i < props.length; i++) { var property = props[i]; confProps[property] = this.options[property] || this.map[property]; } // If numZoomLevels hasn't been set and the maxZoomLevel *has*, // then use maxZoomLevel to calculate numZoomLevels // if ( (!confProps.numZoomLevels) && (confProps.maxZoomLevel) ) { confProps.numZoomLevels = confProps.maxZoomLevel + 1; } // First off, we take whatever hodge-podge of values we have and // calculate/distill them down into a resolutions[] array // if ((confProps.scales != null) || (confProps.resolutions != null)) { //preset levels if (confProps.scales != null) { confProps.resolutions = []; for(var i = 0; i < confProps.scales.length; i++) { var scale = confProps.scales[i]; confProps.resolutions[i] = OpenLayers.Util.getResolutionFromScale(scale, confProps.units); } } confProps.numZoomLevels = confProps.resolutions.length; } else { //maxResolution and numZoomLevels based calculation confProps.resolutions = []; // determine maxResolution if (confProps.minScale) { confProps.maxResolution = OpenLayers.Util.getResolutionFromScale(confProps.minScale, confProps.units); } else if (confProps.maxResolution == "auto") { var viewSize = this.map.getSize(); var wRes = confProps.maxExtent.getWidth() / viewSize.w; var hRes = confProps.maxExtent.getHeight()/ viewSize.h; confProps.maxResolution = Math.max(wRes, hRes); } // determine minResolution if (confProps.maxScale != null) { confProps.minResolution = OpenLayers.Util.getResolutionFromScale(confProps.maxScale); } else if ( (confProps.minResolution == "auto") && (confProps.minExtent != null) ) { var viewSize = this.map.getSize(); var wRes = confProps.minExtent.getWidth() / viewSize.w; var hRes = confProps.minExtent.getHeight()/ viewSize.h; confProps.minResolution = Math.max(wRes, hRes); } // determine numZoomLevels if (confProps.minResolution != null) { var ratio = confProps.maxResolution / confProps.minResolution; confProps.numZoomLevels = Math.floor(Math.log(ratio) / Math.log(2)) + 1; } // now we have numZoomLevels and maxResolution, // we can populate the resolutions array for (var i=0; i < confProps.numZoomLevels; i++) { var res = confProps.maxResolution / Math.pow(2, i) confProps.resolutions.push(res); } } //sort resolutions array ascendingly // confProps.resolutions.sort( function(a, b) { return(b-a); } ); // now set our newly calculated values back to the layer // Note: We specifically do *not* set them to layer.options, which we // will preserve as it was when we added this layer to the map. // this way cloned layers reset themselves to new map div // dimensions) // this.resolutions = confProps.resolutions; this.maxResolution = confProps.resolutions[0]; var lastIndex = confProps.resolutions.length - 1; this.minResolution = confProps.resolutions[lastIndex]; this.scales = []; for(var i = 0; i < confProps.resolutions.length; i++) { this.scales[i] = OpenLayers.Util.getScaleFromResolution(confProps.resolutions[i], confProps.units); } this.minScale = this.scales[0]; this.maxScale = this.scales[this.scales.length - 1]; this.numZoomLevels = confProps.numZoomLevels; }, /** * APIMethod: getResolution * * Returns: * {Float} The currently selected resolution of the map, taken from the * resolutions array, indexed by current zoom level. */ getResolution: function() { var zoom = this.map.getZoom(); return this.resolutions[zoom]; }, /** * APIMethod: getExtent * * Returns: * {<OpenLayers.Bounds>} A Bounds object which represents the lon/lat * bounds of the current viewPort. */ getExtent: function() { // just use stock map calculateBounds function -- passing no arguments // means it will user map's current center & resolution // return this.map.calculateBounds(); }, /** * APIMethod: getZoomForExtent * * Parameters: * bounds - {<OpenLayers.Bounds>} * closest - {Boolean} Find the zoom level that most closely fits the * specified bounds. Note that this may result in a zoom that does * not exactly contain the entire extent. * Default is false. * * Returns: * {Integer} The index of the zoomLevel (entry in the resolutions array) * for the passed-in extent. We do this by calculating the ideal * resolution for the given extent (based on the map size) and then * calling getZoomForResolution(), passing along the 'closest' * parameter. */ getZoomForExtent: function(extent, closest) { var viewSize = this.map.getSize(); var idealResolution = Math.max( extent.getWidth() / viewSize.w, extent.getHeight() / viewSize.h ); return this.getZoomForResolution(idealResolution, closest); }, /** * Method: getDataExtent * Calculates the max extent which includes all of the data for the layer. * This function is to be implemented by subclasses. * * Returns: * {<OpenLayers.Bounds>} */ getDataExtent: function () { //to be implemented by subclasses }, /** * APIMethod: getZoomForResolution * * Parameters: * resolution - {Float} * closest - {Boolean} Find the zoom level that corresponds to the absolute * closest resolution, which may result in a zoom whose corresponding * resolution is actually smaller than we would have desired (if this * is being called from a getZoomForExtent() call, then this means that * the returned zoom index might not actually contain the entire * extent specified... but it'll be close). * Default is false. * * Returns: * {Integer} The index of the zoomLevel (entry in the resolutions array) * that corresponds to the best fit resolution given the passed in * value and the 'closest' specification. */ getZoomForResolution: function(resolution, closest) { var diff; var minDiff = Number.POSITIVE_INFINITY; for(var i=0; i < this.resolutions.length; i++) { if (closest) { diff = Math.abs(this.resolutions[i] - resolution); if (diff > minDiff) { break; } minDiff = diff; } else { if (this.resolutions[i] < resolution) { break; } } } return Math.max(0, i-1); }, /** * APIMethod: getLonLatFromViewPortPx * * Parameters: * viewPortPx - {<OpenLayers.Pixel>} * * Returns: * {<OpenLayers.LonLat>} An OpenLayers.LonLat which is the passed-in * view port <OpenLayers.Pixel>, translated into lon/lat by the layer. */ getLonLatFromViewPortPx: function (viewPortPx) { var lonlat = null; if (viewPortPx != null) { var size = this.map.getSize(); var center = this.map.getCenter(); if (center) { var res = this.map.getResolution(); var delta_x = viewPortPx.x - (size.w / 2); var delta_y = viewPortPx.y - (size.h / 2); lonlat = new OpenLayers.LonLat(center.lon + delta_x * res , center.lat - delta_y * res); if (this.wrapDateLine) { lonlat = lonlat.wrapDateLine(this.maxExtent); } } // else { DEBUG STATEMENT } } return lonlat; }, /** * APIMethod: getViewPortPxFromLonLat * * Parameters: * lonlat - {<OpenLayers.LonLat>} * * Returns: * {<OpenLayers.Pixel>} An <OpenLayers.Pixel> which is the passed-in * <OpenLayers.LonLat>,translated into view port pixels. */ getViewPortPxFromLonLat: function (lonlat) { var px = null; if (lonlat != null) { var resolution = this.map.getResolution(); var extent = this.map.getExtent(); px = new OpenLayers.Pixel( Math.round(1/resolution * (lonlat.lon - extent.left)), Math.round(1/resolution * (extent.top - lonlat.lat)) ); } return px; }, /** * APIMethod: setOpacity * Sets the opacity for the entire layer (all images) * * Parameter: * opacity - {Float} */ setOpacity: function(opacity) { if (opacity != this.opacity) { this.opacity = opacity; for(var i=0; i<this.div.childNodes.length; ++i) { var element = this.div.childNodes[i].firstChild; OpenLayers.Util.modifyDOMElement(element, null, null, null, null, null, null, opacity); } } }, /** * Method: setZIndex * * Parameters: * zIndex - {Integer} */ setZIndex: function (zIndex) { this.div.style.zIndex = zIndex; }, /** * Method: adjustBounds * This function will take a bounds, and if wrapDateLine option is set * on the layer, it will return a bounds which is wrapped around the * world. We do not wrap for bounds which *cross* the * maxExtent.left/right, only bounds which are entirely to the left * or entirely to the right. * * Parameters: * bounds - {<OpenLayers.Bounds>} */ adjustBounds: function (bounds) { if (this.gutter) { // Adjust the extent of a bounds in map units by the // layer's gutter in pixels. var mapGutter = this.gutter * this.map.getResolution(); bounds = new OpenLayers.Bounds(bounds.left - mapGutter, bounds.bottom - mapGutter, bounds.right + mapGutter, bounds.top + mapGutter); } if (this.wrapDateLine) { // wrap around the date line, within the limits of rounding error var wrappingOptions = { 'rightTolerance':this.getResolution() }; bounds = bounds.wrapDateLine(this.maxExtent, wrappingOptions); } return bounds; }, CLASS_NAME: "OpenLayers.Layer"});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -