这个Control的初始化需要传入一个VectorLayer和一个MousePosition控件,当用户按下Ctrl+V时,自动复制VectorLayer层的第一个选中对象并且将之拷贝到鼠标在地图上的当前位置(从MousePosition获得)
OpenLayers.Control.CopyControl = OpenLayers.Class(OpenLayers.Control, {
initialize: function(vectorLayer, mousePositionCtl) {
this.layer = vectorLayer;
this.mousePositionCtl = mousePositionCtl;
OpenLayers.Control.prototype.initialize.apply(this, arguments);
},
destroy: function() {
if (this.handler) {
this.handler.destroy();
}
this.handler = null;
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
draw: function() {
this.handler = new OpenLayers.Handler.Keyboard( this, {
"keydown": this.defaultKeyPress }, {
"keyMask": OpenLayers.Handler.MOD_CTRL
});
this.activate();
},
defaultKeyPress: function (evt) {
switch(evt.keyCode) {
case 86:
var vector = this.layer.selectedFeatures[0];
if(vector) {
var lonLat = this.layer.map.getLonLatFromPixel(this.mousePositionCtl.lastXy);
var newVector = vector.clone();
this.layer.addFeatures([newVector]);
newVector.move(lonLat);
}
}
},
CLASS_NAME: "OpenLayers.Control.CopyControl"
});