Labels for KML Layers

2289
3
Jump to solution
04-04-2017 10:02 AM
MichaelLacy
New Contributor III

Hi, adding a KMLLayer to the map doesn't seem to display any labels.  (There was a similar question about this, but it is a few years old and never really answered.)  Anyway, since KML layers are converted to feature layers, I thought I could set the labeling info on the constituent feature layers by doing something like this:

kmlLayer.on("load"function() {     
    var labelingInfo = [{
        "labelExpressionInfo": {
            "expression""$feature.name"
        },
        "labelPlacement""below-right"
    }];
    var layers = kmlLayer.getLayers();
    for (var i = 0; i < layers.length; i++) {
        if (layer instanceof FeatureLayer) {
            layer.setLabelingInfo(labelingInfo);
        }
     }
});

I don't get any errors if I do this, but I also don't get any labels.  Should this work for the feature layers created from a KML layer?  If not, is there a correct way to add labels to KML layers?

Thanks,

Mike

0 Kudos
1 Solution

Accepted Solutions
MichaelLacy
New Contributor III

Ugh!  Stupid error on my part.  I didn't have "showLabels: true" when creating the map.  What I had works now.

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Mike,

   This is what worked for me in the past:

layer.on("load",  lang.hitch(this, function()  {
  var json = {
    "labelExpressionInfo": {
      "value": "{name}"
    },
    "useCodedValues": false,
    "fieldInfos": [{
      fieldName: "name",
    }]
  };
   var layers = layer.getLayers();
   //create instance of LabelClass
   var lc = new LabelClass(json);
   lc.symbol = new TextSymbol();
   layers[0].setLabelingInfo([lc]);
 }));
MichaelLacy
New Contributor III

Ugh!  Stupid error on my part.  I didn't have "showLabels: true" when creating the map.  What I had works now.

0 Kudos
igemetristechnologies
New Contributor II

Hello. This post is pretty old, but I'm interested in it.
I'm loading a KML file given by URL at startup (only from given domain).
I tried this code in order to auto-display label, but it doesn't work.
I lost pretty much time with this, but could not find a solution.

I guess I should use GeoJSON in order to create better "drawings", but if you have an idea what could be missing... ?

Best regards

hasLabels is true
showLayers is true
labelingInfo seems to be ok

labelingInfo: Array(1)

0:

fieldInfos: [{…}]

labelExpressionInfo: {value: "aaa {name}"}

labelPlacement: null

symbol: {type: "textsymbol", x: 0, y: 0, text: "", rotated: false, …}

useCodedValues: false

__proto__: Object

length: 1

0 Kudos