[OpenLayers-Users] Click on OpenLayers.Feature.Vector

Andreas Hocevar ahocevar at opengeo.org
Thu Feb 25 14:37:25 EST 2010


Christoph Lingg wrote:
> Hi Andreas,
> 
> thank you for your help!
> 
> I have alreade tried to use the SelectFeature class. But a strange
> behaviour appears when combing it with the DragFeature: i can click
> the feature only once. If i don't use the DragFeature the
> SelectFeature works like expected. I created a working example of
> that phenomena, see at the bottom. If you disable the dragFeature the
> selectFeature works like expected, maybe this is even a bug...?

Not necessarily a bug, but an annoyance that has to do with the way 
OpenLayers controls work.

One proper way to deal with this would be to have the DragFeature 
control deactivated by default, and activate it in the featureselected 
handler. In the onComplete callback of the DragFeature control, you can 
deactivate the DragFeature control and re-activate the SelectFeature 
control.

Regards,
Andreas.

> 
> Cheers, Christoph
> 
> layer = new OpenLayers.Layer.Vector("layer"); map.addLayers([layer]);
> 
> 
> drag = new OpenLayers.Control.DragFeature(layer); 
> map.addControl(drag); drag.activate();
> 
> click = new OpenLayers.Control.SelectFeature( [layer], { clickout:
> true, toggle: false, multiple: false, hover: false, toggleKey:
> "ctrlKey", // ctrl key removes from selection multipleKey: "shiftKey"
> // shift key adds to selection } ); map.addControl(click); 
> layer.events.on({ "featureselected": function(e) { alert('selected');
>  }, "featureunselected": function(e) { alert('unselected'); } }); 
> click.activate();
> 
> ftr = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,
> 2)); // an icon layer.addFeatures([ftr]);
> 
> 
> ----- Original Message ----- From: ahocevar at opengeo.org To:
> christoph at lingg.eu Date: 25.02.2010 14:36:48 Subject: Re:
> [OpenLayers-Users] Click on OpenLayers.Feature.Vector
> 
> 
>> Hi,
>> 
>> you need to create an OpenLayers.Control.SelectFeature instance, 
>> configure it with your vector layer, and add it to the map. You can
>> then listen for the layer's "featureselected" event.
>> 
>> See the [1] example if you need a code snippet. The example does
>> exactly the same, but on two different layers instead of just one.
>> 
>> Regards, Andreas.
>> 
>> [1]
>> http://openlayers.org/dev/examples/select-feature-multilayer.html
>> 
>> Christoph Lingg wrote:
>>> Hello!
>>> 
>>> I changed from GoogleMap to OpenLayers, and although I enjoy to
>>> use the opensource equivalent, sometimes I feel lost in the OL
>>> documentation ;-)
>>> 
>>> Would anyone be so kind and could give me hint how to achieve
>>> this very low level problem: I have a Vector Layer (I can't use
>>> marker class as I use the dragging feature as well) to which I
>>> add some Feature Vector, that are displayed on the map. What I
>>> need now is triggering an event when a feature was clicked. Very
>>> basically my code looks like this:
>>>> layer = new OpenLayers.Layer.Vector("layer"); 
>>>> map.addLayers([layer]); ftr = new OpenLayers.Feature.Vector(new
>>>> OpenLayers.Geometry.Point(lon, lat), null, styleMarker); // an
>>>> icon layer.addFeatures([ftr]);
>>> I tried different approaches that never worked out! What is the
>>> right way to go?
>>> 
>>> Thank you in advance for any help!
>>> 
>>> Christoph
>>> 
>>> 
>>> 
>>> _______________________________________________ Users mailing
>>> list Users at openlayers.org 
>>> http://openlayers.org/mailman/listinfo/users
>> 
>> -- Andreas Hocevar OpenGeo - http://opengeo.org/ Expert service
>> straight from the developers.
>> 
> 
> 
> 


-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Users mailing list