box.js

来自「用来在地图上做操作GIS,在地图上做标记」· JavaScript 代码 · 共 156 行

JS
156
字号
/* 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/Handler.js * @requires OpenLayers/Handler/Drag.js *  * Class: OpenLayers.Handler.Box * Handler for dragging a rectangle across the map.  Box is displayed  * on mouse down, moves on mouse move, and is finished on mouse up. * * Inherits from: *  - <OpenLayers.Handler>  */OpenLayers.Handler.Box = OpenLayers.Class(OpenLayers.Handler, {    /**      * Property: dragHandler      * {<OpenLayers.Handler.Drag>}      */    dragHandler: null,    /**     * Constructor: OpenLayers.Handler.Box     *     * Parameters:     * control - {<OpenLayers.Control>}      * callbacks - {Object} An object containing a single function to be     *                          called when the drag operation is finished.     *                          The callback should expect to recieve a single     *                          argument, the point geometry.     * options - {Object}      */    initialize: function(control, callbacks, options) {        OpenLayers.Handler.prototype.initialize.apply(this, arguments);        var callbacks = {            "down": this.startBox,             "move": this.moveBox,             "out":  this.removeBox,            "up":   this.endBox        };        this.dragHandler = new OpenLayers.Handler.Drag(                                this, callbacks, {keyMask: this.keyMask});    },    /**     * Method: setMap     */    setMap: function (map) {        OpenLayers.Handler.prototype.setMap.apply(this, arguments);        if (this.dragHandler) {            this.dragHandler.setMap(map);        }    },    /**    * Method: startBox    *    * Parameters:    * evt - {Event}     */    startBox: function (xy) {        this.zoomBox = OpenLayers.Util.createDiv('zoomBox',                                                 this.dragHandler.start,                                                 null,                                                 null,                                                 "absolute",                                                 "2px solid red");        this.zoomBox.style.backgroundColor = "white";        this.zoomBox.style.filter = "alpha(opacity=50)"; // IE        this.zoomBox.style.opacity = "0.50";        this.zoomBox.style.fontSize = "1px";        this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;        this.map.viewPortDiv.appendChild(this.zoomBox);        // TBD: use CSS classes instead        this.map.div.style.cursor = "crosshair";    },    /**    * Method: moveBox    */    moveBox: function (xy) {        var deltaX = Math.abs(this.dragHandler.start.x - xy.x);        var deltaY = Math.abs(this.dragHandler.start.y - xy.y);        this.zoomBox.style.width = Math.max(1, deltaX) + "px";        this.zoomBox.style.height = Math.max(1, deltaY) + "px";        if (xy.x < this.dragHandler.start.x) {            this.zoomBox.style.left = xy.x+"px";        }        if (xy.y < this.dragHandler.start.y) {            this.zoomBox.style.top = xy.y+"px";        }    },    /**    * Method: endBox    */    endBox: function(end) {        var result;        if (Math.abs(this.dragHandler.start.x - end.x) > 5 ||                Math.abs(this.dragHandler.start.y - end.y) > 5) {               var start = this.dragHandler.start;            var top = Math.min(start.y, end.y);            var bottom = Math.max(start.y, end.y);            var left = Math.min(start.x, end.x);            var right = Math.max(start.x, end.x);            result = new OpenLayers.Bounds(left, bottom, right, top);        } else {            result = this.dragHandler.start.clone(); // i.e. OL.Pixel        }         this.removeBox();        // TBD: use CSS classes instead        this.map.div.style.cursor = "";        this.callback("done", [result]);    },    /**     * Method: removeBox     * Remove the zoombox from the screen and nullify our reference to it.     */    removeBox: function() {        this.map.viewPortDiv.removeChild(this.zoomBox);        this.zoomBox = null;    },    /**     * Method: activate     */    activate: function () {        if (OpenLayers.Handler.prototype.activate.apply(this, arguments)) {            this.dragHandler.activate();            return true;        } else {            return false;        }    },    /**     * Method: deactivate     */    deactivate: function () {        if (OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {            this.dragHandler.deactivate();            return true;        } else {            return false;        }    },    CLASS_NAME: "OpenLayers.Handler.Box"});

⌨️ 快捷键说明

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