Extract <name> attribute from KML
        Posted  
        
            by Ozaki
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ozaki
        
        
        
        Published on 2010-06-16T04:11:48Z
        Indexed on 
            2010/06/16
            4:12 UTC
        
        
        Read the original article
        Hit count: 509
        
I am using OpenLayers for a mapping service.
In which I have several KML layers that are using KML feeds from server to populate data on the map.
It currently plots: images / points / vector lines & shapes.
But on these points it will not add a label with the value of the for the placemark in the KML.
What I have currently tried is:
////////////////////KML Feed for * Layer//
                    var surveylinelayer = new OpenLayers.Layer.Vector("First KML Layer", {
                        projection: new OpenLayers.Projection("EPSG:4326"),
                        strategies: [new OpenLayers.Strategy.Fixed()],
                        protocol: new OpenLayers.Protocol.HTTP({
                            url: firstKMLURL,
                            format: new OpenLayers.Format.KML({
                                extractStyles: true,
                                extractAttributes: true
                            })
                        }),
                        styleMap: new OpenLayers.StyleMap({
                            "default": KMLStyle
                        })
                    });
then the Style as follows:
var KMLStyle = new OpenLayers.Style({
                        //label: "${name}", // This method will display nothing
                        fillOpacity: 1,
                        pointRadius: 10,
                        fontColor: "#7E3C1C",
                        fontSize: "13px",
                        fontFamily: "Courier New, monospace",
                        fontWeight: "strong",
                        labelXOffset: "0",
                        labelYOffset: "-15"
                }, {
                    //dynamic label
                    context: {
                        label: function(feature) {
                            return "Feature Name: " + feature.attributes.name; // also displays nothing
                        }
                        }
                });
Example of the KML:
        <Placemark>
            <name>POI1</name>
            <Style>
                <LabelStyle>
                    <color>ffffffff</color>
                </LabelStyle>
            </Style>
            <Point>
                <coordinates>0.000,0.000</coordinates>
            </Point>
        </Placemark>
When debugging I just hit "feature is undefined" and am unsure why it would be undefined in this instance?
© Stack Overflow or respective owner