Displaying Label/Text for POI fails (using KML)

347 views
Skip to first unread message

Urban Kägi

unread,
Jun 26, 2014, 9:06:04 AM6/26/14
to geoadm...@googlegroups.com
Hello

We try to show a text on each icon we display for a cluster of POIs .The label displays the number of POIs which are contained in the Cluster.

The data for the POIs is transferred as KML from the server to the client.

Whatever we tried, we couldnt find a way to show a label on our cluster icons (see attached screenshot).

We defined a style for the cluster icons in KML, which works fine:
<Document>
 <Folder>
  <name>Title</name>
  <Style id="styleCluster">
   <IconStyle id="clusterIcon">
     <Icon>
    <href>/images/marker.png</href>
     </Icon>
   </IconStyle>
  </Style>
  [...]

Can you give us a hint, how we can display a string for each POI? Can we do this in KML or do we have to do it clientside "on-the-fly"? How?

Thank you very much in advance & best regards

Urban
cluster.png

Olivier Terral

unread,
Jun 27, 2014, 2:52:56 AM6/27/14
to geoadm...@googlegroups.com
Hi,

Currently, <LabelStyle> is not supported by the KML parser of Openlayers 3.

You need to do it manually, using a styleFunction on your vector layer :

// Create the KML Layer
var vector = new ol.layer.Vector({
  source: new ol.source.KML({
    projection: 'EPSG:21781',
    url: 'bln-style.kml'
  }),
  style: function(feature, resolution) {
    var styleFromKml = feature.getStyleFunction().call(feature, resolution);
    var textStyle = new ol.style.Text({
      text: feature.get('name'),
      color: '#ff0000',
      fontFamily: 'Arial',
      fontSize: 11,
      opacity: 0.6
    });
    return [styleFromKml, textStyle]
  }
});


I've not tested this code but this should work.

Hope this helps.

viktor.fa...@bitbee.ch

unread,
Jun 27, 2014, 5:39:27 AM6/27/14
to geoadm...@googlegroups.com
Hello,

thank you for your quick Response. If we try this code we get following JavaScript errormessage:

  1. Uncaught TypeError: Cannot read property 'Yf' of undefined ga.js:345


Tnank you very much für your help!

Best regrads

Viktor

Olivier Terral

unread,
Jun 27, 2014, 7:43:20 AM6/27/14
to geoadm...@googlegroups.com, viktor.fa...@bitbee.ch
Now it's tested : http://jsfiddle.net/5JVDm/1/

:)

Olivier Terral

unread,
Jun 27, 2014, 7:51:03 AM6/27/14
to geoadm...@googlegroups.com, viktor.fa...@bitbee.ch

viktor.fa...@bitbee.ch

unread,
Jun 27, 2014, 8:24:49 AM6/27/14
to geoadm...@googlegroups.com, viktor.fa...@bitbee.ch
Hi,

thank you so much for your help!
Now it is working perfect.

Best regards

Viktor


Am Freitag, 27. Juni 2014 13:51:03 UTC+2 schrieb Olivier Terral:

sergio...@ingenia-telecom.com

unread,
Jan 21, 2015, 9:58:39 AM1/21/15
to geoadm...@googlegroups.com, viktor.fa...@bitbee.ch
Hello,

The example is not working with the last version :(

The 'style' function is not getting called when 'extractStyles' is true (default) in ol.source.KML, but when I set it to false, the 'feature.getStyleFunction()' is undefined.

Thanks in advance.

Olivier Terral

unread,
Jan 21, 2015, 10:44:32 AM1/21/15
to geoadm...@googlegroups.com
Hi Sergio,

With the last version of OpenlLayers the priority of style functions has been inverted before it was Layer -> Source -> Feature  now it's Feature -> Source -> Layer so if a style is defined for a feature (it's the case when extractStyles is true) the layer style will not be used that's why the layer's styleFunction is not used.


In the case of extractStyles=false  no styles will be defined for the features so the feature.getStyleFunction is null and because there is no styles defined for the feature the layer's styleFunction is used.


Here a working codepen : http://codepen.io/oterral/pen/uHdaw


--
You received this message because you are subscribed to the Google Groups "GeoAdmin API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geoadmin-api...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages