📄 map.js
字号:
/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license. * See http://svn.openlayers.org/trunk/openlayers/release-license.txt * for the full text of the license. *//** * @requires OpenLayers/Util.js * @requires OpenLayers/Events.js * * Class: OpenLayers.Map * Instances of OpenLayers.Map are interactive maps embedded in a web page. * Create a new map with the <OpenLayers.Map> constructor. * * On their own maps do not provide much functionality. To extend a map * it's necessary to add controls (<OpenLayers.Control>) and * layers (<OpenLayers.Layer>) to the map. */OpenLayers.Map = OpenLayers.Class({ /** * Constant: Z_INDEX_BASE * {Object} Base z-indexes for different classes of thing */ Z_INDEX_BASE: { BaseLayer: 100, Overlay: 325, Popup: 750, Control: 1000 }, /** * Constant: EVENT_TYPES * {Array(String)} supported application event types */ EVENT_TYPES: [ "addlayer", "removelayer", "changelayer", "movestart", "move", "moveend", "zoomend", "popupopen", "popupclose", "addmarker", "removemarker", "clearmarkers", "mouseover", "mouseout", "mousemove", "dragstart", "drag", "dragend", "changebaselayer"], /** * Property: id * {String} Unique identifier for the map */ id: null, /** * APIProperty: events * {<OpenLayers.Events>} An events object that handles all * events on the map */ events: null, /** * APIProperty: div * {DOMElement} The element that contains the map */ div: null, /** * Property: size * {<OpenLayers.Size>} Size of the main div (this.div) */ size: null, /** * Property: viewPortDiv * {HTMLDivElement} The element that represents the map viewport */ viewPortDiv: null, /** * Property: layerContainerOrigin * {<OpenLayers.LonLat>} The lonlat at which the later container was * re-initialized (on-zoom) */ layerContainerOrigin: null, /** * Property: layerContainerDiv * {HTMLDivElement} The element that contains the layers. */ layerContainerDiv: null, /** * Property: layers * {Array(<OpenLayers.Layer>)} Ordered list of layers in the map */ layers: null, /** * Property: controls * {Array(<OpenLayers.Control>)} List of controls associated with the map */ controls: null, /** * Property: popups * {Array(<OpenLayers.Popup>)} List of popups associated with the map */ popups: null, /** * APIProperty: baseLayer * {<OpenLayers.Layer>} The currently selected base layer. This determines * min/max zoom level, projection, etc. */ baseLayer: null, /** * Property: center * {<OpenLayers.LonLat>} The current center of the map */ center: null, /** * Property: zoom * {Integer} The current zoom level of the map */ zoom: 0, /** * Property: viewRequestID * {String} Used to store a unique identifier that changes when the map * view changes. viewRequestID should be used when adding data * asynchronously to the map: viewRequestID is incremented when * you initiate your request (right now during changing of * baselayers and changing of zooms). It is stored here in the * map and also in the data that will be coming back * asynchronously. Before displaying this data on request * completion, we check that the viewRequestID of the data is * still the same as that of the map. Fix for #480 */ viewRequestID: 0, // Options /** * APIProperty: tileSize * {<OpenLayers.Size>} Set in the map options to override the default tile * size for this map. */ tileSize: null, /** * APIProperty: projection * {String} Set in the map options to override the default projection * string this map - also set maxExtent, maxResolution, and * units if appropriate. */ projection: "EPSG:4326", /** * APIProperty: units * {String} The map units. Defaults to 'degrees'. Possible values are * 'degrees' (or 'dd'), 'm', 'ft', 'km', 'mi', 'inches'. */ units: 'degrees', /** * APIProperty: resolutions * {Array(Float)} A list of map resolutions (map units per pixel) in * descending order. If this is not set in the layer constructor, it * will be set based on other resolution related properties * (maxExtent, maxResolution, maxScale, etc.). */ resolutions: null, /** * APIProperty: maxResolution * {Float} Default max is 360 deg / 256 px, which corresponds to * zoom level 0 on gmaps. Specify a different value in the map * options if you are not using a geographic projection and * displaying the whole world. */ maxResolution: 1.40625, /** * APIProperty: minResolution * {Float} */ minResolution: null, /** * APIProperty: maxScale * {Float} */ maxScale: null, /** * APIProperty: minScale * {Float} */ minScale: null, /** * APIProperty: maxExtent * {<OpenLayers.Bounds>} The maximum extent for the map. Defaults to the * whole world in decimal degrees * (-180, -90, 180, 90). Specify a different * extent in the map options if you are not using a * geographic projection and displaying the whole * world. */ maxExtent: null, /** * APIProperty: minExtent * {<OpenLayers.Bounds>} */ minExtent: null, /** * APIProperty: restrictedExtent * {<OpenLayers.Bounds>} Limit map navigation to this extent where possible. * If a non-null restrictedExtent is set, panning will be restricted * to the given bounds. In addition, zooming to a resolution that * displays more than the restricted extent will center the map * on the restricted extent. If you wish to limit the zoom level * or resolution, use maxResolution. */ restrictedExtent: null, /** * APIProperty: numZoomLevels * {Integer} Number of zoom levels for the map. Defaults to 16. Set a * different value in the map options if needed. */ numZoomLevels: 16, /** * APIProperty: theme * {String} Relative path to a CSS file from which to load theme styles. * Specify null in the map options (e.g. {theme: null}) if you * want to get cascading style declarations - by putting links to * stylesheets or style declarations directly in your page. */ theme: null, /** * APIProperty: fallThrough * {Boolean} Should OpenLayers allow events on the map to fall through to * other elements on the page, or should it swallow them? (#457) * Default is to swallow them. */ fallThrough: false, /** * Constructor: OpenLayers.Map * Constructor for a new OpenLayers.Map instance. * * Parameters: * div - {String} Id of an element in your page that will contain the map. * options - {Object} Optional object with properties to tag onto the map. * * Examples: * (code) * // create a map with default options in an element with the id "map1" * var map = new OpenLayers.Map("map1"); * * // create a map with non-default options in an element with id "map2" * var options = { * maxExtent: new OpenLayers.Bounds(-200000, -200000, 200000, 200000), * maxResolution: 156543, * units: 'meters', * projection: "EPSG:41001" * }; * var map = new OpenLayers.Map("map2", options); * (end) */ initialize: function (div, options) { //set the default options this.setOptions(options); this.id = OpenLayers.Util.createUniqueID("OpenLayers.Map_"); this.div = OpenLayers.Util.getElement(div); // the viewPortDiv is the outermost div we modify var id = this.div.id + "_OpenLayers_ViewPort"; this.viewPortDiv = OpenLayers.Util.createDiv(id, null, null, null, "relative", null, "hidden"); this.viewPortDiv.style.width = "100%"; this.viewPortDiv.style.height = "100%"; this.viewPortDiv.className = "olMapViewport"; this.div.appendChild(this.viewPortDiv); // the layerContainerDiv is the one that holds all the layers id = this.div.id + "_OpenLayers_Container"; this.layerContainerDiv = OpenLayers.Util.createDiv(id); this.layerContainerDiv.style.zIndex=this.Z_INDEX_BASE['Popup']-1; this.viewPortDiv.appendChild(this.layerContainerDiv); this.events = new OpenLayers.Events(this, this.div, this.EVENT_TYPES, this.fallThrough); this.updateSize(); // update the map size and location before the map moves this.events.register("movestart", this, this.updateSize); // Because Mozilla does not support the "resize" event for elements // other than "window", we need to put a hack here. if (OpenLayers.String.contains(navigator.appName, "Microsoft")) { // If IE, register the resize on the div this.events.register("resize", this, this.updateSize); } else { // Else updateSize on catching the window's resize // Note that this is ok, as updateSize() does nothing if the // map's size has not actually changed. OpenLayers.Event.observe(window, 'resize', OpenLayers.Function.bind(this.updateSize, this)); } // only append link stylesheet if the theme property is set if(this.theme) { // check existing links for equivalent url var addNode = true; var nodes = document.getElementsByTagName('link'); for(var i=0; i<nodes.length; ++i) { if(OpenLayers.Util.isEquivalentUrl(nodes.item(i).href, this.theme)) { addNode = false; break; } } // only add a new node if one with an equivalent url hasn't already // been added if(addNode) { var cssNode = document.createElement('link'); cssNode.setAttribute('rel', 'stylesheet'); cssNode.setAttribute('type', 'text/css'); cssNode.setAttribute('href', this.theme); document.getElementsByTagName('head')[0].appendChild(cssNode); } } this.layers = []; if (this.controls == null) { if (OpenLayers.Control != null) { // running full or lite? this.controls = [ new OpenLayers.Control.Navigation(), new OpenLayers.Control.PanZoom(), new OpenLayers.Control.ArgParser(), new OpenLayers.Control.Attribution() ]; } else { this.controls = []; } } for(var i=0; i < this.controls.length; i++) { this.addControlToMap(this.controls[i]); } this.popups = []; this.unloadDestroy = OpenLayers.Function.bind(this.destroy, this); // always call map.destroy() OpenLayers.Event.observe(window, 'unload', this.unloadDestroy); }, /** * Method: unloadDestroy * Function that is called to destroy the map on page unload. stored here * so that if map is manually destroyed, we can unregister this. */ unloadDestroy: null, /** * APIMethod: destroy * Destroy this map */ destroy:function() { // if unloadDestroy is null, we've already been destroyed if (!this.unloadDestroy) { return false; } // map has been destroyed. dont do it again! OpenLayers.Event.stopObserving(window, 'unload', this.unloadDestroy); this.unloadDestroy = null; if (this.layers != null) { for (var i = this.layers.length - 1; i>=0; --i) { //pass 'false' to destroy so that map wont try to set a new // baselayer after each baselayer is removed this.layers[i].destroy(false); } this.layers = null; } if (this.controls != null) { for (var i = this.controls.length - 1; i>=0; --i) { this.controls[i].destroy(); } this.controls = null; } if (this.viewPortDiv) { this.div.removeChild(this.viewPortDiv); } this.viewPortDiv = null; this.events.destroy(); this.events = null; }, /** * APIMethod: setOptions * Change the map options * * Parameters: * options - {Object} Hashtable of options to tag to the map */ setOptions: function(options) { // Simple-type defaults are set in class definition. // Now set complex-type defaults this.tileSize = new OpenLayers.Size(OpenLayers.Map.TILE_WIDTH, OpenLayers.Map.TILE_HEIGHT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -