modifyfeature.js

来自「用来在地图上做操作GIS,在地图上做标记」· JavaScript 代码 · 共 562 行 · 第 1/2 页

JS
562
字号
/* Copyright (c) 2006 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/Control/DragFeature.js * @requires OpenLayers/Control/SelectFeature.js * @requires OpenLayers/Handler/Keyboard.js *  * Class: OpenLayers.Control.ModifyFeature * Control to modify features.  When activated, a click renders the vertices *     of a feature - these vertices can then be dragged.  By default, the *     delete key will delete the vertex under the mouse.  New features are *     added by dragging "virtual vertices" between vertices.  Create a new *     control with the <OpenLayers.Control.ModifyFeature> constructor. * * Inherits From: *  - <OpenLayers.Control> */OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, {    /**     * APIProperty: geometryTypes     * {Array(String)} To restrict modification to a limited set of geometry     *     types, send a list of strings corresponding to the geometry class     *     names.     */    geometryTypes: null,    /**     * Property: layer     * {<OpenLayers.Layer.Vector>}     */    layer: null,        /**     * Property: feature     * {<OpenLayers.Feature.Vector>} Feature currently available for modification.     */    feature: null,        /**     * Property: vertices     * {Array(<OpenLayers.Feature.Vector>)} Verticies currently available     *     for dragging.     */    vertices: null,        /**     * Property: virtualVertices     * {Array(<OpenLayers.Feature.Vector>)} Virtual vertices in the middle     *     of each edge.     */    virtualVertices: null,    /**     * Property: selectControl     * {<OpenLayers.Control.Select>}     */    selectControl: null,        /**     * Property: dragControl     * {<OpenLayers.Control.DragFeature>}     */    dragControl: null,        /**     * Property: keyboardHandler     * {<OpenLayers.Handler.Keyboard>}     */    keyboardHandler: null,        /**     * APIProperty: deleteCodes     * {Array(Integer)} Keycodes for deleting verticies.  Set to null to disable     *     vertex deltion by keypress.  If non-null, keypresses with codes     *     in this array will delete vertices under the mouse. Default     *     is 46 and 100, the 'delete' and lowercase 'd' keys.     */    deleteCodes: null,            radius:0.0,        centerlonlat:null,            origin:null,        angle:0.0,            /**     * APIProperty: virtualStyle     * {<OpenLayers.Feature.Vector.Style>}     */    virtualStyle: null,        /**     * APIProperty: onModificationStart      * {Function} Optional function to be called when a feature is selected     *     to be modified. The function should expect to be called with a     *     feature.  This could be used for example to allow to lock the     *     feature on server-side.     */    onModificationStart: function() {},    /**     * APIProperty: onModification     * {Function} Optional function to be called when a feature has been     *     modified.  The function should expect to be called with a feature.     */    onModification: function() {},    /**     * APIProperty: onModificationEnd     * {Function} Optional function to be called when a feature is finished      *     being modified.  The function should expect to be called with a     *     feature.     */    onModificationEnd: function() {},    /**     * Constructor: OpenLayers.Control.ModifyFeature     * Create a new modify feature control.     *     * Parameters:     * layer - {<OpenLayers.Layer.Vector>} Layer that contains features that     *     will be modified.     * options - {Object} Optional object whose properties will be set on the     *     control.     */    initialize: function(layer, options) {        this.layer = layer;        this.vertices = [];        this.virtualVertices = [];        this.styleVirtual = OpenLayers.Util.extend({}, this.layer.style);        this.styleVirtual.fillOpacity = 0.3;        this.styleVirtual.strokeOpacity = 0.3;        this.deleteCodes = [46, 100];        OpenLayers.Control.prototype.initialize.apply(this, [options]);        if(!(this.deleteCodes instanceof Array)) {            this.deleteCodes = [this.deleteCodes];        }        var control = this;                // configure the select control        var selectOptions = {            geometryTypes: this.geometryTypes,                        onSelect: function(feature){               OpenLayers.Control.ModifyFeature.feature=feature;               alert(feature.id+ "  "+feature.geometry.getBounds().radius);               this.radius=OpenLayers.Control.ModifyFeature.feature.geometry.getBounds().radius;               this.centerlonlat=OpenLayers.Control.ModifyFeature.feature.geometry.getBounds().getCenterLonLat();                              OpenLayers.Control.ModifyFeature.origin= new OpenLayers.Geometry.Point(this.centerlonlat.lon, this.centerlonlat.lat);                              OpenLayers.Control.ModifyFeature.angle=Math.PI * ((1/OpenLayers.Control.ModifyFeature.feature.geometry.getBounds().sides) - (1/2));                                                                                          control.selectFeature.apply(control, [feature]);                                           },            onUnselect: function(feature) {               alert(OpenLayers.Control.ModifyFeature.feature.id+" "+OpenLayers.Control.ModifyFeature.feature.geometry.getBounds().radius);                control.unselectFeature.apply(control, [feature]);                            }        };        this.selectControl = new OpenLayers.Control.SelectFeature(            layer, selectOptions        );        // configure the drag control        var dragOptions = {            geometryTypes: ["OpenLayers.Geometry.Point"],            snappingOptions: this.snappingOptions,                        onStart: function(feature, pixel) {             // alert(OpenLayers.Control.ModifyFeature.feature.id+" "+OpenLayers.Control.ModifyFeature.feature.geometry.getBounds().radius);              this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});                           // alert(OpenLayers.Control.ModifyFeature.origin);              var maploc = this.map.getLonLatFromPixel(pixel);//当前点中的坐标                            //this.layer              // control.dragStart.apply(control, [feature, pixel]);                                            },              onDrag: function(feature,pixel){                              var maploc = this.map.getLonLatFromPixel(pixel);                var point = new OpenLayers.Geometry.Point(maploc.lon, maploc.lat);                var alpha = Math.atan2(point.y - OpenLayers.Control.ModifyFeature.origin.y,                               point.x - OpenLayers.Control.ModifyFeature.origin.x);                                  //设置半径                               OpenLayers.Control.ModifyFeature.radius=Math.max(this.map.getResolution() / 2,                                   point.distanceTo(OpenLayers.Control.ModifyFeature.origin));                                   OpenLayers.Control.ModifyFeature.modifyGeometry();                 var style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});                                var feature = new OpenLayers.Feature.Vector(OpenLayers.Control.ModifyFeature.feature.geometry.clone());                editLayer.drawFeature(OpenLayers.Control.ModifyFeature.feature, style);                editLayer.addFeatures([OpenLayers.Control.ModifyFeature.feature]);                                                                              /// this.layer.drawFeature(this.feature, this.style);                                 //  control.dragVertex.apply(control, [feature]);                                                },              onComplete: function(feature,pixel) {                                // control.dragComplete.apply(control, [feature]);                                            }        };        this.dragControl = new OpenLayers.Control.DragFeature(            layer, dragOptions        );        // configure the keyboard handler        var keyboardOptions = {            keypress: this.handleKeypress        };        this.keyboardHandler = new OpenLayers.Handler.Keyboard(            this, keyboardOptions        );    },    /**     * APIMethod: destroy     * Take care of things that are not handled in superclass.     */    destroy: function() {        this.layer = null;        this.selectControl.destroy();        this.dragControl.destroy();        this.keyboardHandler.destroy();        OpenLayers.Control.prototype.destroy.apply(this, []);    },    /**     * APIMethod: activate     * Activate the control and the feature handler.     *      * Returns:     * {Boolean} Successfully activated the control and feature handler.     */    activate: function() {        return (this.selectControl.activate() &&                this.keyboardHandler.activate() &&                OpenLayers.Control.prototype.activate.apply(this, arguments));    },    /**     * APIMethod: deactivate     * Deactivate the controls.     *     * Returns:      * {Boolean} Successfully deactivated the control.     */    deactivate: function() {        var deactivated = false;        // the return from the controls is unimportant in this case        if(OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {            this.layer.removeFeatures(this.vertices);            this.layer.removeFeatures(this.virtualVertices);

⌨️ 快捷键说明

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