[OpenLayers-Users] Text labels
Paul Spencer
pagameba at gmail.com
Fri Oct 2 08:22:33 EDT 2009
Jan, default is a reserved keyword in javascript, you will note in my
example that it is quoted :) When specifying the name of object
properties in object literals, you don't normally have to quote them
unless the name is a reserved word.
var styleMap = new OpenLayers.StyleMap({
'default': OpenLayers.Util.applyDefaults({
})
});
Cheers
Paul
On 2009-10-02, at 8:12 AM, Nicholas Efremov-Kendall wrote:
> Hi all,
>
> First, thanks Paul and Jan for your very helpful emails. I've
> decided to try to go with Paul's method first, and if I understand
> correctly this should be it: However I get a parse error on the
> third line (default: OpenLayers...). I'm not sure what's going on
> here. Any suggestions?
>
> var styleMap = new OpenLayers.StyleMap(
> {
> default: OpenLayers.Util.applyDefaults(
> {
> label: '${Name}',
> strokeWidth:1,
> strokeColor:"#CCCCCC",
> fillColor: "green"
> })
> }
> );
>
> var cities = new OpenLayers.Layer.GML ("7th Century
> Cities", "data/cities.gml",
> {
> styleMap: styleMap,
> projection: new OpenLayers.Projection("EPSG:4326"),
> format: OpenLayers.Format.GML
> }
> );
> map.addLayer(cities);
>
> On Tue, Sep 29, 2009 at 3:57 PM, Paul Spencer <pagameba at gmail.com>
> wrote:
> A simpler styleMap would be to take the Name directly from the
> feature's attributes, but using a context to provide functions that
> calculate per-feature styles is very powerful
>
>
> var styleMap = new OpenLayers.StyleMap({
> default: OpenLayers.Util.applyDefaults({
> label: '${Name}', // this comes from feature.attributes.Name and
> is case sensitive
>
> strokeWidth:1,
> strokeColor:"#CCCCCC",
> fillColor: "green"
> })
> });
>
> Cheers
>
> Paul
>
>
> On 2009-09-29, at 5:28 AM, Jan H. van der Ven wrote:
>
> Dear Nicholas,
>
>
> I use a StyleMap like so:
>
> var styleMap = new OpenLayers.StyleMap(
> {
> "default":OpenLayers.Util.applyDefaults(
> {
> fillColor: stateColors[layerIndex],
> fillOpacity: 0.4,
> strokeColor: stateColors[layerIndex],
> strokeWidth: 2,
> label : "${getName}", // see context below
> labelAlign: "cm",
> fontColor: "yellow",
> fontSize: "12px",
> fontFamily: "Verdana",
> fontWeight: "bold"
> }
> ,OpenLayers.Feature.Vector.style["default"])
>
> ,"select":OpenLayers.Util.applyDefaults
> ({},OpenLayers.Feature.Vector.style["select"])
> });
> // the labelFunction prevents the modify handles being labeled with
> "undefined"
> styleMap.styles["default"].context={getName: labelFunction};
>
> This StyleMap is then added as a parameter to the layer:
> var laag = new OpenLayers.Layer.GML(
> layerName,
> url,
> {
> format:OpenLayers.Format.GeoJSON,
> visibility:false,
> styleMap:styleMap,
> isBaseLayer:false,
> });
>
> And the labelFunction is used to show the label only at a certain zoom
> level:
>
> function labelFunction(feature){
> if (map.getZoom()>4){
> if (feature.attributes.name != undefined){
> return feature.attributes.name;
> }else{
> return '';
> }
> }else{
> return '';
> }
> }
>
> The part about undefined has to do with edit handles.
>
> Kind regards,
>
>
> Jan
>
> Nicholas Efremov-Kendall wrote:
> Hi all,
>
> Sorry to bug you all with a pretty simple question. I have a shp file
> converted to GML, with an attribute called "name." I'd like for this
> to be places as a label by the polygon feature. I think, based on my
> digging around on the lists that I need a function to first get the
> feature.attributes.Name <http://feature.attributes.Name>, which would
> then pass it to the label. Does anyone have a good example of this, or
> suggestions on implementation?
>
> var cities = new OpenLayers.Layer.GML ("7th Century Cities",
> "data/cities.gml",
> {
> style: {strokeWidth:1, strokeColor:"#CCCCCC", fillColor: "green"},
> projection: new OpenLayers.Projection("EPSG:4326"),
> format: OpenLayers.Format.GML,
>
> label: "$name",
>
>
> }
> );
> map.addLayer(cities);
>
>
> --
> Nicholas Efremov-Kendall
> Fulbright Student 2009-2010, Ukraine
> nefremov at artsci.wustl.edu <mailto:nefremov at artsci.wustl.edu>
> c/o Halyna Yerko
> Balzaka 92a, Kv 27
> 02232
> Kyiv, Ukraine
> (mob) +380963576524
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.5.409 / Virus Database: 270.13.114/2401 - Release Date:
> 09/28/09 17:53:00
>
>
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
>
>
>
> --
> Nicholas Efremov-Kendall
> Fulbright Student 2009-2010, Ukraine
> nefremov at artsci.wustl.edu
> c/o Halyna Yerko
> Balzaka 92a, Kv 27
> 02232
> Kyiv, Ukraine
> (mob) +380963576524
More information about the Users
mailing list