Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

thanks

Dennis

The offending kml file is at:

https://docs.google.com/open?id=0BxhE8rRos0u1X2ExNldTdWwwWkk

Thanks for looking!

Dennis

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

share|improve this question
    
Could you share kml file or your live example? –  drnextgis Jul 16 '12 at 2:24
    
Please accept an answer if it was helpful. –  drnextgis Jul 18 '12 at 2:07
add comment

3 Answers 3

up vote 4 down vote accepted

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

OpenLayers.Util.extend(
    OpenLayers.Feature.Vector.style.default,
    {
        label: "${getLabel}",
        pointRadius: 15
    }
);

var mystyle = new OpenLayers.Style(
    OpenLayers.Feature.Vector.style.default,
    {
        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()
        })
});
map.addLayer(myKML);

See live demo here.

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

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.

Dennis

share|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
add comment

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

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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