[OpenLayers-Users] Highlighting a feature?

Tim Schaub tschaub at openplans.org
Fri Nov 30 13:17:25 EST 2007


Hey again-

Tim Schaub wrote:
> Hey-
> 
> Gregor Mosheh wrote:
>> A request has come up to highlight one or more polygon features, e.g. 
>> search results or the like. What would be an advisable way of doing this?
>>
>> Using plain Mapserver, we used additional query args and a filter, to 
>> create a layer which would only show only those features in some ungodly 
>> highlight-worthy color. The WMS server is still Mapserver, and I'd not 
>> be averse to using a OL.Mapserver layer.
>>
>> Alternately, I imagine that one could fetch the feature with WFS and 
>> draw it in some ungodly highlight-worthy color.
>>
>> And if there's a wiki page or example that I missed, even better!
>>
> 
> Depends what you mean by a feature.
> 
> If you're rendering vector features on the client side, the 
> SelectFeature control is all about clicking on or hovering over a 
> feature to select it (add it to the layer's selectedFeatures array and 
> draw it with a select style).
> 
> If this fits your case, see: 
> http://openlayers.org/dev/examples/select-feature.html
> 
> You can also use the SelectFeature control to select a feature 
> programmatically.  That is
> 
> control.select(feature);
> control.unselect(feature);
> 
> If you are rendering your features server side (and you want to keep 
> doing this), then you can "select" with SLD via WMS.  This of course 
> presumes that you know enough about what feature you want to select to 
> create a filter.
> 
> Of course, GetFeatureInfo provides a nice way to get feature attributes 
> based on pixel location (see 
> http://openlayers.org/dev/examples/getfeatureinfo.html).  Two requests 
> just to highlight a selected feature is a bit clunky (one to get feature 
> info, one to apply SLD to a GetMap request).  And who likes creating all 
> that sld/filter stuff anyway?  Easier just to get your feature client 
> side and style it however you like.
> 
> For docs on the SelectFeature control, see
> http://dev.openlayers.org/apidocs/files/OpenLayers/Control/SelectFeature-js.html
> 
> Tim

And as crschmidt just mentioned to me - maybe I should make it clear 
that you can also listen for a click on the map, fetch a feature, and 
then add it to your vector layer.

Starting with that GetFeatureInfo example, you should be able to see how 
you could turn that in to a WFS GetFeature request.  Nothing canned, 
you'll have to do the work yourself.

Once you have the feature (you can use the OpenLayers.Format.GML class 
to parse it), you can add it to a vector layer with:

layer.addFeatures([feature]);

And of course you'll probably want to give it some style first.

Tim




> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> !DSPAM:4033,4750510893151637810514!
> 




More information about the Users mailing list