{"id":21,"date":"2014-04-02T18:42:00","date_gmt":"2014-04-03T00:42:00","guid":{"rendered":""},"modified":"2017-02-02T23:41:52","modified_gmt":"2017-02-03T05:41:52","slug":"adding-icon-labels-to-openlayers-with-kml-styles","status":"publish","type":"post","link":"https:\/\/trash80.org\/blog\/?p=21","title":{"rendered":"Adding Icon Labels to Openlayers with KML styles"},"content":{"rendered":"<p>I&#8217;ve been working on a project that uses OpenLayers, KML, Extjs4, GeoExt, and a few other libraries.&nbsp; On this project, customers would like to use a map to track assets.&nbsp; They want to see a specific icon for a type of asset with a label below the icon.&nbsp; What we found was that OpenLayers doesn&#8217;t support KML styles along with OpenLayers styleMap on a Vector layer.<\/p>\n<p>Searching online turned up a handful of individuals with the same issue, but no solution.   Here is how I solved it: <\/p>\n<p>Problematic Code example: <br \/>Setup a styleMap: <\/p>\n<pre>var styleMap = new OpenLayers.StyleMap({'default':{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;label : \"${name}\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;fontColor: \"#000000\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;fontSize: \"11px\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;labelAlign: \"center\",<br \/>&nbsp;&nbsp;&nbsp;&nbsp;pointRadius: 2,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;labelXOffset : -16,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;labelYOffset : 0,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;labelSelect: true<br \/>}});<br \/><\/pre>\n<p>Setup to get and parse KML: <\/p>\n<pre><br \/>var myKmlFormat = new OpenLayers.Format.KML({<br \/>&nbsp;&nbsp;&nbsp;&nbsp;internalProjection: sphericalMercatorProj,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;externalProjection: mapProj,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;extractStyles: true,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;extractAttributes: true,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;maxDepth: 2<br \/>});<br \/><\/pre>\n<p>Finally, setup the vector layer: <\/p>\n<pre><br \/>var layer = new OpenLayers.Layer.Vector(\"KML\", {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;projection: latLonProj,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;strategies: [new OpenLayers.Strategy.Fixed()],<br \/>&nbsp;&nbsp;&nbsp;&nbsp;protocol: new OpenLayers.Protocol.HTTP({<br \/>&nbsp;&nbsp;&nbsp;&nbsp;url: myKmlUrl,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;format: myKmlForma <br \/>&nbsp;&nbsp;&nbsp;&nbsp;}),<br \/>&nbsp;&nbsp;&nbsp;&nbsp;styleMap: kmlStyleMap,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;eventListeners: { 'loadend': kmlLoaded },<br \/>&nbsp;&nbsp;&nbsp;&nbsp;renderers: renderer<br \/>});<br \/><\/pre>\n<p>The KML: <\/p>\n<pre><span style=\"font-size: x-small;\"> &lt;document&gt;<br \/>&nbsp;&lt;name&gt;Geolocation Case: 17564&lt;\/name&gt;<br \/>&nbsp;&nbsp;&lt;style id=\"style1\"&gt;<br \/>&nbsp;&nbsp;&nbsp;&lt;iconstyle&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;icon&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;href&gt;http:\/\/127.0.0.1:8000\/static\/images\/pointicon.png&lt;\/href&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/Icon&gt;<br \/>&nbsp;&nbsp;&nbsp;&lt;\/IconStyle&gt;<br \/>&nbsp;&nbsp;&lt;\/style&gt;<br \/>&nbsp;&nbsp;&lt;placemark&gt;<br \/>&nbsp;&nbsp;&nbsp;&lt;name&gt;My Point&lt;\/name&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;point&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;coordinates&gt;-81.522832781,-3.33918759741,0.0&lt;\/coordinates&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/point&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;lookat&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;longitude&gt;-81.522832781&lt;\/longitude&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;latitude&gt;-3.33918759741&lt;\/latitude&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/lookat&gt;<br \/>&nbsp;&nbsp;&nbsp;&lt;styleurl&gt;#style1&lt;\/styleurl&gt;<br \/>&nbsp;&nbsp;&lt;\/placemark&gt;<br \/>&lt;\/document&gt;<br \/><\/span><\/pre>\n<p>Google Earth will use the Placemark Name as the label, OpenLayers does not.&nbsp; The line &#8220;extractStyles: true,&#8221; in the OpenLayers KML section tells OpenLayers to parse the styles in the KML.&nbsp; Setting it to false tells OpenLayers to ignore the style sections of the KML.<\/p>\n<p>When this boolean is set to true, OpenLayers.Format.KML ignores manually added styleMap, such as the line &#8220;styleMap: kmlStyleMap&#8221;.&nbsp; So if your wanting a label on your placemark, it won&#8217;t happen.&nbsp; It is an &#8216;or&#8217;, not an &#8216;and&#8217;.&nbsp; This is a bug in OpenLayers in my opinion.<\/p>\n<p>So, how do you get around this issue and add a label?&nbsp; Well, read on.<\/p>\n<p>Here is the Solution Code: <\/p>\n<p>First off, don&#8217;t create a styleMap and add it to the Vector constructor.&nbsp; Add a listener to the layer:<\/p>\n<pre>layer.events.on({<br \/>&nbsp;&nbsp;loadend : this.addLabel<br \/>});<br \/><\/pre>\n<p>Now, go through each icon (feature) and give the style a label, an offset, and anything else your label may need. <\/p>\n<pre>addLabel : function(event) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;var features = event.object.features;<br \/><br \/>&nbsp;&nbsp;&nbsp;&nbsp;for (var i = 0, len = features.length; i &lt; len; i++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var style = features[i]['style'];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var attributes = features[i]['attributes'];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!Ext.isEmpty(style) &amp;&amp; !Ext.isEmpty(attributes)){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style.label = attributes.name;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style.labelYOffset = -16;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style.fontSize = '11px';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style.fontColor = '#000000';<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;style.labelSelect = true;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;event.object.redraw();<br \/>}<br \/><\/pre>\n<p>The icons on the map should now have labels.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a project that uses OpenLayers, KML, Extjs4, GeoExt, and a few other libraries.&nbsp; On this project, customers would like to use a map to track assets.&nbsp; They want to see a specific icon for a type of asset with a label below the icon.&nbsp; What we found was that OpenLayers doesn&#8217;t &hellip; <a href=\"https:\/\/trash80.org\/blog\/?p=21\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Adding Icon Labels to Openlayers with KML styles<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":1,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":40,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions\/40"}],"wp:attachment":[{"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trash80.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}