OpenLayers: Zoom when a feature in a vector layer is double-clicked
In OpenLayers, double-clicking on the map, by default, zooms in. If you add features (OpenLayers.Feature.Vector) to a layer (OpenLayers.Layer.Vector), double-clicking on the features will not zoom in. In order to re-enable double-click zoom in this case, you’ll need to add a custom control to the map.
var DblclickFeature = OpenLayers.Class(OpenLayers.Control, {
initialize: function (layer, options) {
OpenLayers.Control.prototype.initialize.apply(this, [ options ]);
this.handler = new OpenLayers.Handler.Feature(this, layer, {
dblclick: this.dblclick
});
}
});
So far, we’ve defined a control class that handles double-clicks on features. Let’s instantiate the control and define its double-click handler (map is your instance of OpenLayers.Map and vectorLayer is your instance of OpenLayers.Layer.Vector):
var dblclick = new DblclickFeature(vectorLayer, {
dblclick: function (event) {
map.zoomIn();
}
});
Lastly, let’s add it to the map and activate it:
map.addControl(dblclick);
dblclick.activate();