[OpenLayers-Users] Problem with click event in mobile browsers

Richard Greenwood richard.greenwood at gmail.com
Sun Jul 17 18:48:43 EDT 2011


On Sun, Jul 17, 2011 at 4:41 PM, Christian Spanring <cspanring at gmail.com> wrote:
> Hi,
>
> I'm having problems with a 'click' event in mobile browsers. A 'click'
> event registered with
>
>    map.events.register('click', map, clickEvent);
>
> doesn't seem to fire in default browsers on Android 2.3, 3.2 and iOS
> 4.2 (works without error on desktop browsers).
>
> I'm trying to read the coordinates a user 'tapped' on a map with something like
>
>    function clickEvent(e) {
>        var clickCoord = e.object.getLonLatFromPixel(e.xy);
>    }
>
> without much luck so far.
>
> Is there something fundamentally wrong with my approach or is this a
> known issue with mobile browsers?
>
> I'm working with OpenLayers 2.x at 0b9555a4c81fb1f94d56.
>
> Thanks!
> Christian

Christian,

I won't say that this is the right way, but it does work for me.

1. Create a click handler. This is basically just a copy and paste from
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
    );
  }
});


2. Use the click handler:

var click = new OpenLayers.Control.Click( { trigger: function(e) {
    /* do your thing */
}

Other may be able to provide a bteer approach.

Rich

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


More information about the Users mailing list