Reputation: 11
I am trying to display feature name in the map while loading (ex: name of the building).
How we can display this in OpenLayers by using GeoServer.
Thanks, Ashok
Upvotes: 1
Views: 1843
Reputation: 578
If you are using WMS layers, try with SLD styles. But,if you are using vector layers, you must to use OpenLayers StyleMap to label your features, try this
var style = new OpenLayers.StyleMap({
default :new OpenLayers.Style({
'label': '${feature_attr}'
})
})
Upvotes: 0
Reputation: 31
You need to define TextSymbolizer for the feature using styles in geoserver. Go to geoserver admin --> Styles --> Add a new style.
Add a TextSymblolizer as follows
<sld:NamedLayer xmlns:sld="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">
<sld:UserStyle>
<sld:Name>layer_name</sld:Name>
<sld:Title>layer_title</sld:Title>
<sld:Abstract/>
<sld:FeatureTypeStyle>
<sld:FeatureTypeName>Feature</sld:FeatureTypeName>
<sld:Rule>
<sld:Name>rule01</sld:Name>
<sld:TextSymbolizer>
<sld:Label>
<ogc:PropertyName>feature_name</ogc:PropertyName>
</sld:Label>
<sld:Font>
<sld:CssParameter name="font-family">Arial</sld:CssParameter>
<sld:CssParameter name="font-style">Normal</sld:CssParameter>
<sld:CssParameter name="font-size">12</sld:CssParameter>
</sld:Font>
<sld:LabelPlacement>
<sld:PointPlacement>
<sld:AnchorPoint>
<sld:AnchorPointX>
0.5
</sld:AnchorPointX>
<sld:AnchorPointY>
0.5
</sld:AnchorPointY>
</sld:AnchorPoint>
</sld:PointPlacement>
</sld:LabelPlacement>
</sld:TextSymbolizer>
</sld:Rule>
</sld:FeatureTypeStyle>
</sld:UserStyle>
</sld:NamedLayer>
Within the tag define the feature whose value needs to be displayed. Next associate the given layer with this style, update and reload.
Upvotes: 0