⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 map.js

📁 用来在地图上做操作GIS,在地图上做标记
💻 JS
📖 第 1 页 / 共 4 页
字号:
    */    removePopup: function(popup) {        OpenLayers.Util.removeItem(this.popups, popup);        if (popup.div) {            try { this.layerContainerDiv.removeChild(popup.div); }            catch (e) { } // Popups sometimes apparently get disconnected                      // from the layerContainerDiv, and cause complaints.        }        popup.map = null;    },  /********************************************************/  /*                                                      */  /*              Container Div Functions                 */  /*                                                      */  /*   The following functions deal with the access to    */  /*    and maintenance of the size of the container div  */  /*                                                      */  /********************************************************/         /**     * APIMethod: getSize     *      * Returns:     * {<OpenLayers.Size>} An <OpenLayers.Size> object that represents the      *                     size, in pixels, of the div into which OpenLayers      *                     has been loaded.      *                     Note - A clone() of this locally cached variable is     *                     returned, so as not to allow users to modify it.     */    getSize: function () {        var size = null;        if (this.size != null) {            size = this.size.clone();        }        return size;    },    /**     * APIMethod: updateSize     * This function should be called by any external code which dynamically     *     changes the size of the map div (because mozilla wont let us catch      *     the "onresize" for an element)     */    updateSize: function() {        // the div might have moved on the page, also        this.events.element.offsets = null;        var newSize = this.getCurrentSize();        var oldSize = this.getSize();        if (oldSize == null)            this.size = oldSize = newSize;        if (!newSize.equals(oldSize)) {                        // store the new size            this.size = newSize;            //notify layers of mapresize            for(var i=0; i < this.layers.length; i++) {                this.layers[i].onMapResize();                            }            if (this.baseLayer != null) {                var center = new OpenLayers.Pixel(newSize.w /2, newSize.h / 2);                var centerLL = this.getLonLatFromViewPortPx(center);                var zoom = this.getZoom();                this.zoom = null;                this.setCenter(this.getCenter(), zoom);            }        }    },        /**     * Method: getCurrentSize     *      * Returns:     * {<OpenLayers.Size>} A new <OpenLayers.Size> object with the dimensions      *                     of the map div     */    getCurrentSize: function() {        var size = new OpenLayers.Size(this.div.clientWidth,                                        this.div.clientHeight);        // Workaround for the fact that hidden elements return 0 for size.        if (size.w == 0 && size.h == 0 || isNaN(size.w) && isNaN(size.h)) {            var dim = OpenLayers.Element.getDimensions(this.div);            size.w = dim.width;            size.h = dim.height;        }        if (size.w == 0 && size.h == 0 || isNaN(size.w) && isNaN(size.h)) {            size.w = parseInt(this.div.style.width);            size.h = parseInt(this.div.style.height);        }        return size;    },    /**      * Method: calculateBounds     *      * Parameters:     * center - {<OpenLayers.LonLat>} Default is this.getCenter()     * resolution - {float} Default is this.getResolution()      *      * Returns:     * {<OpenLayers.Bounds>} A bounds based on resolution, center, and      *                       current mapsize.     */    calculateBounds: function(center, resolution) {        var extent = null;                if (center == null) {            center = this.getCenter();        }                        if (resolution == null) {            resolution = this.getResolution();        }            if ((center != null) && (resolution != null)) {            var size = this.getSize();            var w_deg = size.w * resolution;            var h_deg = size.h * resolution;                    extent = new OpenLayers.Bounds(center.lon - w_deg / 2,                                           center.lat - h_deg / 2,                                           center.lon + w_deg / 2,                                           center.lat + h_deg / 2);                }        return extent;    },  /********************************************************/  /*                                                      */  /*            Zoom, Center, Pan Functions               */  /*                                                      */  /*    The following functions handle the validation,    */  /*   getting and setting of the Zoom Level and Center   */  /*       as well as the panning of the Map              */  /*                                                      */  /********************************************************/    /**     * APIMethod: getCenter     *      * Returns:     * {<OpenLayers.LonLat>}     */    getCenter: function () {        return this.center;    },    /**     * APIMethod: getZoom     *      * Returns:     * {Integer}     */    getZoom: function () {        return this.zoom;    },        /**      * APIMethod: pan     * Allows user to pan by a value of screen pixels     *      * Parameters:     * dx - {Integer}     * dy - {Integer}     */    pan: function(dx, dy) {        // getCenter        var centerPx = this.getViewPortPxFromLonLat(this.getCenter());        // adjust        var newCenterPx = centerPx.add(dx, dy);                // only call setCenter if there has been a change        if (!newCenterPx.equals(centerPx)) {            var newCenterLonLat = this.getLonLatFromViewPortPx(newCenterPx);            this.setCenter(newCenterLonLat);        }   },    /**     * APIMethod: setCenter     *      * Parameters:     * lonlat - {<OpenLayers.LonLat>}     * zoom - {Integer}     * dragging - {Boolean} Specifies whether or not to trigger      *                      movestart/end events     * forceZoomChange - {Boolean} Specifies whether or not to trigger zoom      *                             change events (needed on baseLayer change)     *     * TBD: reconsider forceZoomChange in 3.0     */    setCenter: function (lonlat, zoom, dragging, forceZoomChange) {        if (!this.center && !this.isValidLonLat(lonlat)) {            lonlat = this.maxExtent.getCenterLonLat();        }        if(this.restrictedExtent != null) {            // In 3.0, decide if we want to change interpretation of maxExtent.            if(lonlat == null) {                 lonlat = this.getCenter();             }            if(zoom == null) {                 zoom = this.getZoom();             }            var resolution = null;            if(this.baseLayer != null) {                resolution = this.baseLayer.resolutions[zoom];            }            var extent = this.calculateBounds(lonlat, resolution);             if(!this.restrictedExtent.containsBounds(extent)) {                var maxCenter = this.restrictedExtent.getCenterLonLat();                 if(extent.getWidth() > this.restrictedExtent.getWidth()) {                     lonlat = new OpenLayers.LonLat(maxCenter.lon, lonlat.lat);                 } else if(extent.left < this.restrictedExtent.left) {                    lonlat = lonlat.add(this.restrictedExtent.left -                                        extent.left, 0);                 } else if(extent.right > this.restrictedExtent.right) {                     lonlat = lonlat.add(this.restrictedExtent.right -                                        extent.right, 0);                 }                 if(extent.getHeight() > this.restrictedExtent.getHeight()) {                     lonlat = new OpenLayers.LonLat(lonlat.lon, maxCenter.lat);                 } else if(extent.bottom < this.restrictedExtent.bottom) {                     lonlat = lonlat.add(0, this.restrictedExtent.bottom -                                        extent.bottom);                 }                 else if(extent.top > this.restrictedExtent.top) {                     lonlat = lonlat.add(0, this.restrictedExtent.top -                                        extent.top);                 }             }        }                var zoomChanged = forceZoomChange || (                            (this.isValidZoomLevel(zoom)) &&                             (zoom != this.getZoom()) );        var centerChanged = (this.isValidLonLat(lonlat)) &&                             (!lonlat.equals(this.center));        // if neither center nor zoom will change, no need to do anything        if (zoomChanged || centerChanged || !dragging) {            if (!dragging) { this.events.triggerEvent("movestart"); }            if (centerChanged) {                if ((!zoomChanged) && (this.center)) {                     // if zoom hasnt changed, just slide layerContainer                    //  (must be done before setting this.center to new value)                    this.centerLayerContainer(lonlat);                }                this.center = lonlat.clone();            }            // (re)set the layerContainerDiv's location            if ((zoomChanged) || (this.layerContainerOrigin == null)) {                this.layerContainerOrigin = this.center.clone();                this.layerContainerDiv.style.left = "0px";                this.layerContainerDiv.style.top  = "0px";            }            if (zoomChanged) {                this.zoom = zoom;                // zoom level has changed, increment viewRequestID.                this.viewRequestID++;            }                            var bounds = this.getExtent();                        //send the move call to the baselayer and all the overlays                this.baseLayer.moveTo(bounds, zoomChanged, dragging);                        bounds = this.baseLayer.getExtent();                        for (var i = 0; i < this.layers.length; i++) {                var layer = this.layers[i];                if (!layer.isBaseLayer) {                                        var moveLayer;                    var inRange = layer.calculateInRange();                    if (layer.inRange != inRange) {                        // Layer property has changed. We are going                         // to call moveLayer so that the layer can be turned                        // off or on.                           layer.inRange = inRange;                        moveLayer = true;                        this.events.triggerEvent("changelayer");                    } else {                        // If nothing has changed, then we only move the layer                        // if it is visible and inrange.                        moveLayer = (layer.visibility && layer.inRange);                    }                    if (moveLayer) {                        layer.moveTo(bounds, zoomChanged, dragging);                    }                }                            }                        if (zoomChanged) {                //redraw popups                for (var i = 0; i < this.popups.length; i++) {                    this.popups[i].updatePosition();                }            }                            this.events.triggerEvent("move");                if (zoomChanged) { this.events.triggerEvent("zoomend"); }        }        // even if nothing was done, we want to notify of this        if (!dragging) { this.events.triggerEvent("moveend"); }    },    /**      * Method: centerLayerContainer     * This function takes care to recenter the layerContainerDiv.     *      * Parameters:     * lonlat - {<OpenLayers.LonLat>}     */    centerLayerContainer: function (lonlat) {        var originPx = this.getViewPortPxFromLonLat(this.layerContainerOrigin);        var newPx = this.getViewPortPxFromLonLat(lonlat);        if ((originPx != null) && (newPx != null)) {            this.layerContainerDiv.style.left = (originPx.x - newPx.x) + "px";            this.layerContainerDiv.style.top  = (originPx.y - newPx.y) + "px";        }    },    /**     * Method: isValidZoomLevel     *      * Parameters:     * zoomLevel - {Integer}     *      * Returns:     * {Boolean} Whether or not the zoom level passed in is non-null and      *           within the min/max range of zoom levels.     */    isValidZoomLevel: function(zoomLevel) {       return ( (zoomLevel != null) &&                (zoomLevel >= 0) &&                 (zoomLevel < this.getNumZoomLevels()) );    },        /**     * Method: isValidLonLat     *      * Parameters:     * lonlat - {<OpenLayers.LonLat>}     *      * Returns:     * {Boolean} Whether or not the lonlat passed in is non-null and within     *           the maxExtent bounds     */    isValidLonLat: function(lonlat) {        var valid = false;        if (lonlat != null) {            var maxExtent = this.getMaxExtent();            valid = maxExtent.containsLonLat(lonlat);                }        return valid;    },  /********************************************************/  /*                                                      */  /*                 Layer Options                        */  /*                                                      */  /*    Accessor functions to Layer Options parameters    */  /*                                                      */  /********************************************************/        /**     * APIMethod: getProjection     *      * Returns:     * {String} The Projection of the base layer.     */    getProjection: function() {        var projection = null;        if (this.baseLayer != null) {            projection = this.baseLayer.projection;        }        return projection;    },        /**     * APIMethod: getMaxResolution     *      * Returns:     * {String} The Map's Maximum Resolution     */    getMaxResolution: function() {        var maxResolution = null;        if (this.baseLayer != null) {            maxResolution = this.baseLayer.maxResolution;        }        return maxResolution;    },            /**     * APIMethod: getMaxExtent     *      * Returns:     * {<OpenLayers.Bounds>}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -