[GIS] OpenLayers Zoom on map single-click

javascriptopenlayers-2

I am trying to zoom into a location on a single click. How can I zoom towards the point on single click?

I have tried using OpenLayers.Control.ZoomIn and adding a click handler, but for some reason its not registering the "click".

Here is a run-down of my code:

map = ...; // map initialization 
... // added layers

// list of tools for map/feature manipulation
tools = 
{
  // ... tools
  // Tool In question :
  zoomIn : new OpenLayers.Control.ZoomIn({
                // zoom_in needs a click handler, otherwise, its just button on the map
                handler : new OpenLayers.Handler.Click(
                    this, // control
                    {'click' : this.trigger}, // callbacks
                    {'single' : true, 'double' : false, 'pixelTolerance' : 0} // options
                ),
                // overriding ZoomIn.trigger so I can call console.log and alert
                trigger : function(e) {
                    alert("Zoom in, please!");
                    console.log(e);
                    this.map.zoomIn();
                }
          }) // end zoomIn
} // end tools

for(key in tools) {
   map.addControl(tools[key]);
}

// I only made zoomIn active for testing...
tools.zoomIn.activate();

So, the alert doesn't happen and neither does the console.log(e)… How come the callback function is not getting called when I click?

Best Answer

You shouldn't need to use the ZoomIn control. Have a look at the http://openlayers.org/dev/examples/click-handler.html examples to see how to implement a custom handler.