Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm have an OpenLayer map with a single vector layer containing many features. I've been using SelectFeature to pop up some info when a feature is clicked, and that's working fine. However, I now want to add some functionality when the feature is double-clicked - specifically I just want to zoom in, and not have the popup appear.

I gave this a shot: http://blog.slashpoundbang.com/post/2342888467/openlayers-zoom-when-a-feature-in-a-vector-layer-is But it removed my single-click functionality.

Essentially this is the same problem as SelectFeature with Double-Click

Any ideas?

UPDATE:

So I'm trying a workaround using SelectFeature to handle the single click and registering an event listener on the vector layer to handle the double click. All I want to do is zoom in on the double clicked feature. This is what I have:

    VEClayer.events.register('dblclick', VEClayer, function(e){
                    map.zoomToExtent(e.features.geometry.getBounds(), closest=true);
                        }
                        );

However now I have another problem, and I'm not sure if it's being caused by having both SelectFeature and the listener active or what. The new issue is that try as I might I can't access any of the event attributes. So when I try to get e.features.geometry.getBounds(), Firebug gives me the error "e.features is undefined". I've tried to get this multiple other ways (e.xy, e.longlat, getMousePosition) and it's always the same result.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Did you look at OpenLayers.Handler.Click?

The select control takes an handler as option, and the above one has a boolean that should tell you if is single or double click.

Example 

Not exactly what you asked (select on dblClick; deselect on click) but should be easy to change

// Simple WFS layer
wfs = new OpenLayers.Layer.Vector(
    "WFS",
    {
        strategies: [new OpenLayers.Strategy.BBOX()],
        protocol: new OpenLayers.Protocol.WFS(
        {
            url:  "/optima/geoserver/wfs",
            featureType: "node",
            featureNS: "http://piemonte.optima.sistemaits.com",
            geometryName: 'poin'
        })
});
map.addLayer(wfs);

// Add a select control (note: this control is added to the map, but is disabled)
var select = new OpenLayers.Control.SelectFeature(
    wfs,
    {
        multiple: false,
        clickout: true,             // Will be ignored (aka, you have to deselect a feature when no feature is found onClick)
        onSelect: function(feature)
        {
            // ...
        },
        onUnselect: function(feature)
        {
            // ...
        }
    }
);

map.addControl(select);

// Handler to intercept click.
handler = new OpenLayers.Handler.Click(
    select,  // The select control
    {
        click: function(evt)
        {
            console.warn(this); // OpenLayers.Control.SelectFeature
            var feature = this.layer.getFeatureFromEvent(evt);

            // clickout
            if(this.layer.selectedFeatures){
                this.unselect(this.layer.selectedFeatures[0]);
            }
        },
        dblclick: function(evt)
        {
            console.info(this); // OpenLayers.Control.SelectFeature
            var feat = this.layer.getFeatureFromEvent(evt);
            // Select a feature
            if(feat){
                this.select(feat);
            }

        }
    },
    {
        single: true,
        double: true,
        stopDouble: true,
        stopSingle: true
    }
);

// select.activate();  // The select control must NOT be active
handler.activate();    // The handler will handles the click events for the select control
share|improve this answer
    
Beat me to it. (+1) –  CaptDragon Sep 12 '12 at 17:19
    
Thanks a lot for your input, though I'm a little stumped as to how to make it work for me. You mean I can pass an instance of a Click handler in the constructor for my SelectFeature like so? var clickControl = new OpenLayers.Control.Click(); var selectControls = new OpenLayers.Control.SelectFeature(VEClayer,{onSelect: selectFeatureInfo, onUnselect: closeFeatureInfo, handlers: clickControl}); I'm not really sure how to make them work together; would I move my function calls (selectFeatureInfo, closeFeatureInfo) to my click handler? Sorry for my lack of knowledge, I'm new to OpenLayers. –  tairngire Sep 13 '12 at 13:03
    
That example works like a charm. Thanks so much! –  tairngire Sep 14 '12 at 18:14
    
Thank you for that great example! I always get "undefined" from var feature = this.layer.getFeatureFromEvent(evt); I operate the handler on one Vector layer named "kmlLayer". Even if I try var feature = kmlLayer.getFeatureFromEvent(evt); I still get undefined. Any suggestions? –  mephistho Jan 22 '13 at 15:16
    
@mephistho, what's the output of console.log(this)? Did you set the layer for the control? Or maybe could be that the handler is added to the map and activated before the layer is added to the map (the layer should be added before the handler) –  Tommaso Jan 24 '13 at 13:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.