📄 modifyfeature.js
字号:
* * Parameters: * feature - {<OpenLayers.Feature.Vector>} The selected feature. */ selectFeature: function(feature) { this.feature = feature; this.resetVertices(); this.dragControl.activate(); this.onModificationStart(this.feature); }, /** * Method: unselectFeature * Called when the select feature control unselects a feature. * * Parameters: * feature - {<OpenLayers.Feature.Vector>} The unselected feature. */ unselectFeature: function(feature) { this.layer.removeFeatures(this.vertices); this.layer.removeFeatures(this.virtualVertices); this.vertices = []; this.virtualVertices = []; this.feature = null; this.dragControl.deactivate(); this.onModificationEnd(feature); }, /** * Method: dragStart * Called by the drag feature control with before a feature is dragged. * This method is used to differentiate between points and vertices * of higher order geometries. This respects the <geometryTypes> * property and forces a select of points when the drag control is * already active (and stops events from propagating to the select * control). * * Parameters: * feature - {<OpenLayers.Feature.Vector>} The point or vertex about to be * dragged. * pixel - {<OpenLayers.Pixel>} Pixel location of the mouse event. */ dragStart: function(feature, pixel) { // only change behavior if the feature is not in the vertices array if(feature != this.feature && OpenLayers.Util.indexOf(this.vertices, feature) == -1 && OpenLayers.Util.indexOf(this.virtualVertices, feature) == -1) { if(this.feature) { // unselect the currently selected feature this.selectControl.clickFeature.apply(this.selectControl, [this.feature]); } // check any constraints on the geometry type if(this.geometryTypes == null || OpenLayers.Util.indexOf(this.geometryTypes, feature.geometry.CLASS_NAME) != -1) { // select the point this.selectControl.clickFeature.apply(this.selectControl, [feature]); /** * TBD: These lines improve workflow by letting the user * immediately start dragging after the mouse down. * However, it is very ugly to be messing with controls * and their handlers in this way. I'd like a better * solution if the workflow change is necessary. */ // prepare the point for dragging this.dragControl.overFeature.apply(this.dragControl, [feature]); this.dragControl.lastPixel = pixel; this.dragControl.dragHandler.started = true; this.dragControl.dragHandler.start = pixel; this.dragControl.dragHandler.last = pixel; } } }, /** * Method: dragVertex * Called by the drag feature control with each drag move of a vertex. * * Parameters: * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. */ dragVertex: function(vertex) { if(this.feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { if(this.feature != vertex) { this.feature = vertex; } } else { if(OpenLayers.Util.indexOf(this.virtualVertices, vertex) != -1) { vertex.geometry.parent.addComponent(vertex.geometry, vertex._index); delete vertex._index; OpenLayers.Util.removeItem(this.virtualVertices, vertex); this.layer.removeFeatures(vertex); } } this.layer.drawFeature(this.feature, this.selectControl.selectStyle); this.layer.removeFeatures(this.virtualVertices); // keep the vertex on top so it gets the mouseout after dragging // this should be removed in favor of an option to draw under or // maintain node z-index this.layer.drawFeature(vertex); }, /** * Method: dragComplete * Called by the drag feature control when the feature dragging is complete. * * Parameters: * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. */ dragComplete: function(vertex) { this.resetVertices(); this.onModification(this.feature); }, /** * Method: resetVertices */ resetVertices: function() { if(this.vertices.length > 0) { this.layer.removeFeatures(this.vertices); this.vertices = []; } if(this.virtualVertices.length > 0) { this.layer.removeFeatures(this.virtualVertices); this.virtualVertices = []; } if(this.feature && this.feature.geometry.CLASS_NAME != "OpenLayers.Geometry.Point") { this.collectVertices(this.feature.geometry); this.layer.addFeatures(this.vertices); this.layer.addFeatures(this.virtualVertices); } }, /** * Method: handleKeypress * Called by the feature handler on keypress. This is used to delete * vertices and point features. If the <deleteCode> property is set, * vertices and points will be deleted when a feature is selected * for modification and the mouse is over a vertex. * * Parameters: * {Integer} Key code corresponding to the keypress event. */ handleKeypress: function(code) { // check for delete key if(this.feature && OpenLayers.Util.indexOf(this.deleteCodes, code) != -1) { var vertex = this.dragControl.feature; if(vertex && OpenLayers.Util.indexOf(this.vertices, vertex) != -1) { // remove the vertex vertex.geometry.parent.removeComponent(vertex.geometry); this.layer.drawFeature(this.feature, this.selectControl.selectStyle); this.resetVertices(); this.onModification(this.feature); } } }, /** * Method: collectVertices * Collect the vertices from the modifiable feature's geometry and push * them on to the control's vertices array. */ collectVertices: function() { this.vertices = []; this.virtualVirtices = []; var control = this; function collectComponentVertices(geometry) { var i, vertex, component; if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { vertex = new OpenLayers.Feature.Vector(geometry); control.vertices.push(vertex); } else { for(i=0; i<geometry.components.length; ++i) { component = geometry.components[i]; if(component.CLASS_NAME == "OpenLayers.Geometry.Point") { vertex = new OpenLayers.Feature.Vector(component); control.vertices.push(vertex); } else { collectComponentVertices(component); } } // add virtual vertices in the middle of each edge if(geometry.CLASS_NAME != "OpenLayers.Geometry.MultiPoint") { for(i=0; i<geometry.components.length-1; ++i) { var prevVertex = geometry.components[i]; var nextVertex = geometry.components[i + 1]; if(prevVertex.CLASS_NAME == "OpenLayers.Geometry.Point" && nextVertex.CLASS_NAME == "OpenLayers.Geometry.Point") { var x = (prevVertex.x + nextVertex.x) / 2; var y = (prevVertex.y + nextVertex.y) / 2; var point = new OpenLayers.Feature.Vector( new OpenLayers.Geometry.Point(x, y), null, control.styleVirtual ); // set the virtual parent and intended index point.geometry.parent = geometry; point._index = i + 1; control.virtualVertices.push(point); } } } } } collectComponentVertices(this.feature.geometry); }, /** * Method: setMap * Set the map property for the control and all handlers. * * Parameters: * map - {<OpenLayers.Map>} The control's map. */ setMap: function(map) { this.selectControl.setMap(map); this.dragControl.setMap(map); OpenLayers.Control.prototype.setMap.apply(this, arguments); }, CLASS_NAME: "OpenLayers.Control.ModifyFeature"});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -