Labels with KML Polygons

1025
2
09-10-2014 08:39 AM
SamuelHenderson
New Contributor II

Hi All,

I'm using a kml file that was generated by ArcMap.  I'd like to have the polygons in the KML labeled with a value from a field.  What's the best way to do this?  I've followed the sample for labeling things here: https://developers.arcgis.com/javascript/jssamples/layers_label.html and replaced the FeatureLayer with a featureLayer returned from kml.getLayers().  The following Javascript doesn't generate any errors, but unfortunately the labels are still not present.

function LoadKMZ(kmlURL) {    

     var kml = new KMLLayer(kmlURL, { id: "kml" });

     kml.setOpacity(0.5);

     map.addLayer(kml);           

     kml.on("load", function () {

          console.log("KML Loaded");

          var layers = kml.getLayers();

          var fl = layers[0];  // There is only 1 FeatureLayer in the KML

               

          var censusLabel = new TextSymbol().setColor("#666");

          censusLabel.font.setSize("14pt");

          censusLabel.font.setFamily("arial");

          censusLabelRenderer = new SimpleRenderer(censusLabel);

          var labels = new LabelLayer({ id: "labels" });

         labels.addFeatureLayer(fl, censusLabelRenderer, "{" + "LABELNAME"+ "}");

          // add the label layer to the map

          map.addLayer(labels);

     });

}

When the polygons are clicked in the Map a balloon window with the attribute data pops up.  The name I would like to appear in the label layer is a field of this attribute data... but I think what is happening is that on like 15 of the above code when I'm adding the feature layer to my label layer it's not finding the field name.  It's odd that it doesn't give an error.

Tags (3)
0 Kudos
2 Replies
TimWitt2
MVP Alum

Samuel,

labeling has changed with the latest update, please read here What's new in Version 3.10 | Guide | ArcGIS API for JavaScript

  • Labeling expressions no longer use ${field} syntax but now use {field}. In other words, popup template syntax instead of info template syntax.

Tim

0 Kudos
SamuelHenderson
New Contributor II

Hi Tim,

Thanks for the speedy reply!  I don't think I am using the template syntax...  I noticed in the ESRI Sample that the labels had "$" before them on the map so I took the "$" out of my code.  There aren't any errors, but the labels aren't shown either.

0 Kudos