[OpenLayers-Users] Multiple click control stopClick precedence/order

Matthew Williamson matthewdw at gmail.com
Wed May 5 13:12:41 EDT 2010


Hello list,

I'm trying to set up a vector layer to respond to click events, and also
have a map-level click handler that gets fired if the click was not on one
of my features. I'm doing this with two controls, one with a
OpenLayers.Handler.Feature and one with a OpenLayers.Handler.Click.

This is what puzzles me:

In my first attempt: The feature click control is added first, and its
stopSingle value is true. The map click control is added last, and its
stopSingle value is false. When I click on a feature, the handler for the
feature click control fires first (this makes sense), and then the map click
control fires (this didn't make sense to me).

Then, I tried setting stopSingle = true on the map click control as well.
This stopped the feature click control from firing at all! So, I conclude
that--even though the map click control was added last, AND executes last,
the stopSingle value on the map click handler is evaluated first?? Why is
this? How can I ensure that the feature click handler control is able to
stop the propagation to the map click handler, preferably regardless of the
order in which they are added to the map?

To eliminate some confusion: yes, I know about the SelectFeature control. So
far as I can tell, I can't use that--my features are points with large,
mostly transparent icons that tend to overlap and cover the whole map (in
which case SelectFeature intercepts mousedowns on the whole icon and makes
drag-panning impossible), therefore I only want to respond to clicks near
the center (canceling propagation in that case). If someone can tell me how
to make that work with SelectFeature, I'd be glad to use that instead.

Help? Thanks!

-Matt

For reference, here is essentially my code (though the feature click control
gets added in a completely different place, it happens in this order in my
example):

        var vectorLayer = new OpenLayers.Layer.Vector(...);
        /* ...set up vector layer with features, etc... */
        OpenLayers.Control.ClickFeature =
OpenLayers.Class(OpenLayers.Control, {
            defaultHandlerOptions: {
                'single': true,
                'double': false,
                'pixelTolerance': 0,
                'stopSingle': true,
                'stopDouble': false
            },

            initialize: function(options) {
                this.handlerOptions = OpenLayers.Util.extend(
                    {}, this.defaultHandlerOptions
                );
                OpenLayers.Control.prototype.initialize.apply(
                    this, arguments
                );
                this.handler = new OpenLayers.Handler.Feature(
                    this, vectorLayer, {
                        'click': function(feature){
                            if(true){
                                console.log('feature clicked.');
                                OpenLayers.Event.stop(this.handler.evt); //
this shouldn't even be necessary when stopSingle = true, right?
                            }
                        }
                    }, this.handlerOptions
                );
            }
        });
        var featureClickControl = new OpenLayers.Control.ClickFeature();
        map.addControl(featureClickControl);
        featureClickControl.activate();


        // Map click handler
        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': function(evt){console.log('map clicked');}
                    }, this.handlerOptions
                );
            }
        });
        var mapClickControl = new OpenLayers.Control.Click();
        map.addControl(mapClickControl);
        mapClickControl.activate();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20100505/9e60293e/attachment.html


More information about the Users mailing list