[OpenLayers-Users] Showing labels in a KML Vector Layer with extractStyles: true

Chris Wj chris0wj at gmail.com
Fri Mar 9 15:27:21 EST 2012


Hello all. I am having an issue getting a Layer to show labels on the map
along with having it extract styles from a KML file. It shows labels when I
set extractStyles to false, but does not when I set it to true. Is there a
way to show the labels when extractStyles is set to true? Can I enable the
labels after the KML file is loaded? Would this entail something like
modifying the style after the fact?

Example snippit:

var kmlStyleMap = new OpenLayers.StyleMap({'default':{
                        label : "${name}",
                        fontColor: "#000000",
                        fontSize: "11px",
                        labelAlign: "center",
                        pointRadius: 2,
                        labelXOffset : 0,
                        labelYOffset : 0,
                        labelSelect: true
                    }});

var kmlFormat = new OpenLayers.Format.KML({
                    internalProjection: sphericalMercatorProj,
                    externalProjection: mapProj,
                    extractStyles: true,
                    extractAttributes: true,
                    maxDepth: 2
        });

var ellipseKMLLayer = new OpenLayers.Layer.Vector("KML",
            {
                projection: latLonProj,
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: kmlHref,
                    format: kmlFormat
                }),
                styleMap: kmlStyleMap,
                eventListeners: { 'loadend': kmlLoaded },
                renderers: renderer
            });


Sample KML file:

<?xml version="1.0"?>
<kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="
http://www.w3.org/2005/Atom" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Geolocation Case: 17564</name>
    <open>1</open>
    <visibility>1</visibility>
    <Style id="style1">
      <LineStyle>
        <width>2</width>
        <color>bf970952</color>
      </LineStyle>
      <PolyStyle>
        <color>4abf0c74</color>
        <colorMode>random</colorMode>
      </PolyStyle>
      <LabelStyle>
        <color>ffffffff</color>
        <colorMode>normal</colorMode>
        <scale>1.2</scale>
      </LabelStyle>
      <IconStyle>
        <Icon>
          <href>http://127.0.0.1:8000/static/images/pointicon.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Placemark>
      <name>My Point</name>
      <Point>
        <coordinates>-81.522832781,-3.33918759741,0.0</coordinates>
      </Point>
      <LookAt>
        <longitude>-81.522832781</longitude>
        <latitude>-3.33918759741</latitude>
      </LookAt>
      <styleUrl>#style1</styleUrl>
    </Placemark>
    ... more Placemarks ...


-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120309/e7e24f25/attachment.html


More information about the Users mailing list