[OpenLayers-Users] registering a click event on a map

Richard Greenwood richard.greenwood at gmail.com
Tue Sep 11 18:12:11 PDT 2012


On Tue, Sep 11, 2012 at 6:20 PM, Puneet Kishor <punk.kish at gmail.com> wrote:
>
> On Sep 10, 2012, at 7:16 PM, Richard Greenwood <richard.greenwood at gmail.com> wrote:
>
>> On Mon, Sep 10, 2012 at 1:38 PM, Puneet Kishor <punk.kish at gmail.com> wrote:
>>> In OL 2.11 I used to do the following successfully
>>>
>>>    map.events.register(
>>>        "click",
>>>        map,
>>>        function(e) {
>>>            fancyFunction({evt: e, other_params: "foo"});
>>>        }
>>>    );
>>>
>>> The above has stopped working in Firefox (currently using FF v. 15.0.1) without any errors. It does work in Safari 6, but gives the following warning in the console.log
>>>
>>>> event.layerX and event.layerY are broken and deprecated in WebKit. They will be removed from the engine in the near future.
>>>
>>> Suggestions to fix both problems would be welcomed with thanks.
>>
>>
>> I believe the correct way to register a click handler is something like:
>>
>> var click = new OpenLayers.Control.Click( {
>>  trigger: function(e) {
>>    /* do stuff */
>>  }
>> });
>> map.addControl(click);
>> click.activate();
>>
>
>
> Perhaps. But, when I use the above code, I get the following error
>
>         TypeError: OpenLayers.Control.Click is not a constructor

Sorry, I did the same thing the last time I tried to answer this
question. You need to first create a new class:

//   based on:
//   http://dev.openlayers.org/docs/files/OpenLayers/Handler/Click-js.html
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
  defaultHandlerOptions: {
    'single': true,
    'double': false,
    'pixelTolerance': 0,
    'stopSingle': false,
    'stopDouble': false
  },
  initialize: function(options) {
    this.handlerOptions = OpenLayers.Util.extend(
      {}, this.defaultHandlerOptions
    );
    OpenLayers.Control.prototype.initialize.apply(
      this, arguments
    );
    this.handler = new OpenLayers.Handler.Click(
      this, {
        'click': this.trigger
      }, this.handlerOptions
    );
  }
});


Based on my foggy recollection of previous threads I still believe
that the above is the preferred method rather than
    map.events.register("click", ...)
I realize that register("click") exists in examples, but some of them
are pretty dated. Have a look at:
   http://dev.openlayers.org/docs/files/OpenLayers/Handler/Click-js.html

Rich

-- 
Richard Greenwood
richard.greenwood at gmail.com
www.greenwoodmap.com


More information about the Users mailing list