[OpenLayers-Users] Text labels

Jan H. van der Ven j.vanderven at magion.nl
Tue Sep 29 05:28:09 EDT 2009


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
>
>   




More information about the Users mailing list