[OpenLayers-Dev] feature event / highlight a feature

Christine Spietz spietz at fh-bingen.de
Mon Apr 2 05:32:13 EDT 2007


Hi Tim,

I "just" want to register some kind of EventListener to each feature.
The features are all drawn in a layer (it is no matter for me if it is a
Vector-Layer or a GML-Layer or a WFS-Layer or something else).
I want them to be highlighted when they are clicked and to tell me their
fid.

So I tried the following two ways:

###################################1######################################
var polygonLayer = new OpenLayers.Layer.GML("Polygon Layer");

map.addLayer(polygonLayer);


...... //getting the points for the geometry an getting the fid for the
feature

polygonFeature = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.Polygon([linearRing]));
polygonFeature.setFid(slnr.firstChild.nodeValue);
polygonLayer.addFeatures([polygonFeature]);
drawControls = {
    select: new OpenLayers.Control.SelectFeature(polygonLayer),
    hover: new OpenLayers.Control.SelectFeature(polygonLayer,{hover: true})
};
for(key in drawControl)
    map.addControl(drawControls[key]);
    var control = drawControls[key];
    control.activate();
}

In drawControls the select should call a function which highlights the field
as long as no other field is selected and which makes for example an alert
which shows the fid of the feature. If I call a function this way:

drawControls = {
    select: myFunctionToHandleSelection(polygonLayer),
    hover: new OpenLayers.Control.SelectFeature(polygonLayer,{hover: true})
};

I get the following error:

control has no properties
control.outsideViewport = (control.div != null);


Besides that
- I do not know how to get the fid of the selected feature in the function 
myFunctionToHandleSelection(polygonLayer)
- the selected feature only is highlighted for a view seconds and the has 
again his default color.


###################################2 
approach######################################
//in initMap:

url ="/xy?getAllFeatures";
schlaege = new OpenLayers.Layer.GML("GML", url);
map.addLayer(schlaege);
OpenLayers.loadURL(url, "", null, parseData);

...

//in parseData:

function parseData(req) {
g = new OpenLayers.Format.GML();
features = g.read(req.responseText);
for(var feat in features) {
    feature = features[feat];
    feature.events.register("click", feature, function(evt) {
        alert('something');
        Event.stop(evt);}
    );
}
}


There must be an error in code, because the event is not registered, but
unfortunately there also is no error shown on console.

So neither my first nor my second approach is succesfull. The main point is, 
that I am not able to register event-listener (onclick highlight AND give 
some  information) to the several features.


Christine







----- Original Message ----- 
From: "Tim Schaub" <noreply at geocartic.com>
To: <dev at openlayers.org>
Sent: Friday, March 30, 2007 7:44 PM
Subject: Re: [OpenLayers-Dev] feature event / highlight a feature


> Hello Christine-
>
> Christine Spietz wrote:
>> In the example is a vector-Layer and I'm able to paint polygons in the
>> Vector-layer and then select them.
>>
>> But I do not only want to paint polygons, I want to paint "real" features
>> like:
>>
>> <gml:featureMember>
>> ... GML nonsense
>> </gml:featureMember>
>>
>
> If you want use GML to create vector features see:
>
> http://openlayers.org/dev/examples/gml-layer.html
>
> and
>
> http://openlayers.org/dev/examples/GMLParser.html
>
> The first example uses a Layer.GML - a subclass of Layer.Vector.  With
> the GML layer, you construct it with a title and the path to your GML.
> All the features in the GML will be added to your layer.
>
> The second example uses the Format.GML - a GML parser.  In this example,
> you can see how to construct a Format.GML and call the read method to
> get an array of features.  Then you can do whatever you want with these
> features (like add them to a vector layer to have them displayed).
>
> Hope that helps,
>
> Tim
> _______________________________________________
> Dev mailing list
> Dev at openlayers.org
> http://openlayers.org/mailman/listinfo/dev
>




More information about the Dev mailing list