I'm trying to read in mile markers for a marathon from a KML file and use the MILE attribute to set the label. My layer code looks like this:

// add layer
mile_markers_layer = new OpenLayers.Layer.Vector('Mile Markers',
    projection: new OpenLayers.Projection('EPSG:4326'),
    protocol: new OpenLayers.Protocol.HTTP({
        url: 'Mile_Markers.kml',
        format: new OpenLayers.Format.KML({
            extractAttributes: true 
                    //extractStyles:  true
    strategies: [new OpenLayers.Strategy.Fixed()],
            style: {
                'fillColor': '#888888',
                'fillOpacity': 1,
                'fontColor': '#000000',
                'fontFamily': 'arial, sans-serif',
                'fontSize': '.9em',
                'fontWeight': 'bold',
                'label': '${MILE}',
                'pointRadius': 8,
                'strokeColor': '#ff0000',
                'strokeWidth': 3

I looked in Firebug and all the features of the mile_markers_layer have attribute of MILE with a value of "1" thru "26" (as expected).

Layer is displayed with a label of ${MILE} - by this I mean all 26 mile markers are displayed and each has a label of ${MILE}

I was expecting the value of MILE instead. I'm a newbie, so I could be doing something extremely stupid.

The offending KML file is at:


Thanks for the suggestion, but every mile marker ended up with ${MILE.value} for a label.

  • Could you share kml file or your live example? – drnextgis Jul 16 '12 at 2:24

You should use context property of Style object:

options = {
    div: "map",
    zoom: 12,
    center: [-9074392.9993436, 5021122.97485],
    layers: [
        new OpenLayers.Layer.OSM()

map = new OpenLayers.Map(options);

        label: "${getLabel}",
        pointRadius: 15

var mystyle = new OpenLayers.Style(
        context: {
            getLabel: function(f) {
                return f.attributes.MILE.value;

var sm = new OpenLayers.StyleMap({'default': mystyle});

var myKML = new OpenLayers.Layer.Vector(
    "KML layer", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        styleMap: sm,
        projection: new OpenLayers.Projection("EPSG:4326"),
        protocol: new OpenLayers.Protocol.HTTP({
            url: "Mile_Markers.kml",
            format: new OpenLayers.Format.KML()

See live demo here.

| improve this answer | |
  • Thanks drnextgis, I finally figured out my StackExchange account – Dennis Conklin Jul 18 '12 at 23:31

The MILE attribute is an object Object { value="3", displayName="MILE"}

So try doing something like 'label': '${MILE.value}',

| improve this answer | |

Sorry, but my Stack Exchange account has been reset since I logged in using Google - so this is me, Dennis.

Thanks very much drnextgis, that works.

There is a slightly different example I found at: http://openlayers.org/dev/examples/styles-context.html

Once I knew that it was context that I was looking for, it became easier to find some information.

Just as to clarify for those who read this later, I understand it as:

Since my KML feature attributes are OBJECTS and not simple values, the normal '${}' doesn't work - the symbolizer can't assign an object as a label.

The Style Context specifies replacements for the default feature.attributes used for the symbolizer. So you get to write a function that can assign the VALUE of the object, for example, or whatever you want.

Thanks again - I'm definitely a struggling newbie, but now I have numbers for mile markers.


| improve this answer | |
  • drnextgis I wish I could accept your answer, since it was extremely useful, but my old account that I put the question in under has been converted to this new user 8894 once I logged in with Google, and I can't get into my old account (same email). But accept my thanks. – user8894 Jul 18 '12 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.