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

Andreas Hocevar ahocevar at opengeo.org
Thu Feb 25 16:56:06 EST 2010


Christoph Lingg wrote:
> Hi Andreas,
> 
> thank you very much for your help!
> 
> Now it works but I am still not totally satisfied by the solution: I
> think it is not the most intuitive way for endusers to first click a
> feature to be able to drag it then. It might cause some irritation
> and the impression that dragging works not properly. Does somebody
> know an alternative way to achieve the desired behavior? Is this a
> conceptional problem of OpenLayers or is there any chance trying to
> patch this annoyance?

It's conceptual in the way that handlers are not shared among controls. 
But it should be easy to write a combined SelectFeature-DragFeature 
control, by just adding a Drag handler to a SelectFeature control.

There has also been ongoing work on a FeatureAgent (you should find it 
in the archive, and there's also some working code available), which 
would make things like this much easier.

Regards,
Andreas.


> 
> Regards, Christoph
> 
> Am 25.02.2010 um 20:37 schrieb Andreas Hocevar:
> 
>> 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.
> 


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



More information about the Users mailing list