Changing labeling field for LabelLayer (JavaScript)

5091
10
Jump to solution
09-24-2015 10:06 AM
ZoeZaloudek
Occasional Contributor

So – I have a web map that I’ve made from scratch using the ArcGIS API for Javascript.  In this web map I have multiple feature layers (all polygons) that have multiple numeric fields.  Users have the ability to choose what they want to see (in essence, choosing a feature layer and field combination) using dropdowns and radio buttons.

I have figured out how to change the symbology and labels of the polygons shown at any given time.  My problem is with the Label Layer.  The way I have my code written, every time the user changes an option, a new feature layer is added to the Label Layer.  While this ensures that the correct labels are shown, after about a dozen option changes, performance really takes a hit and the view in the web map is glacially slow to update.

Is there a way to just change the field (of the Feature Layer) that the Label Layer is showing without having to add a new feature layer to the Label Layer each time?  I’ve poked around in the documentation for LabelLayer but haven’t found what I’m looking for.  I feel like I’m just missing something obvious here.

Here is the one line that adds the feature layer (the variables lLayer, fLayer, labelRenderer, and field are all calculated before here).  If I comment out this line, everything runs great, but there are no labels.

lLayer.addFeatureLayer(fLayer, labelRenderer, ("{" + field + "}"));

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TimWitt2
MVP Alum

Zoe,

looking at the documentation I think you are right, it looks like you will have to remove the label layer and add a new one once you would like to change the label field.

Tim

View solution in original post

0 Kudos
10 Replies
TimWitt2
MVP Alum

Zoe,

looking at the documentation I think you are right, it looks like you will have to remove the label layer and add a new one once you would like to change the label field.

Tim

0 Kudos
ZoeZaloudek
Occasional Contributor

You know what, that did the trick.  Maybe not the most elegant solution, I'm still open to other ideas, but right now this works so I'm running with it.  Just had to add a few lines of code around my addFeatureLayer line that I'd posted above. 

map.removeLayer(lLayer);
lLayer = new LabelLayer('layerlabel','STATIC');
lLayer.minScale = 10000000;
lLayer.addFeatureLayer(fLayer, labelRenderer, ("{" + field + "}"));
map.addLayer(lLayer);
TimWitt2
MVP Alum

Glad I could help.

It would be nice to have a function to change any parameter of the label layer and then redraw it.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Zoe Zaloudek​ 

You can do this using LabelClass. Here's an example that shows how to create a new label class and apply it to the layer.

JS Bin - Collaborative JavaScript Debugging

ZoeZaloudek
Occasional Contributor

Ah, I see - use LabelClass instead of LabelLayer.  I think I may have initially blown off LabelClass since the documentation seems to be for ArcGISDynamicMapServiceLayer instead of FeatureLayer.

The example is very helpful, I got LabelClass to work with the FeatureLayer in my web map pretty quickly.  Thanks!

Not sure if it's possible to mark two answers as correct in the forums (since Tim's suggestion worked as well)...  But I'm definitely marking this as helpful.

0 Kudos
TracySchloss
Frequent Contributor

Kelly Hutchins

The documentation reads like you can have a stacked label expression, but I don't get how this fits into your example:

labelClass.labelExpression = '[STATE_NAME] CONCAT NEWLINE CONCAT "Population: " CONCAT [POP2007]'

When you construct the LabelClass, you use labelExpressionInfo, which encompasses not only the labelExpression, but also formatting?  I'm having a hard time understanding the Help here.  It seems like there is a lot of potential here, but almost no examples or documentation to follow.

I tried modifying your example to

        var labels = new LabelClass({

        //  labelExpressionInfo: {"value": "{Name}"}

       

        });

        labels.labelExpression = '[Name] CONCAT NEWLINE CONCAT [Magnitude]'

but just ended up with nothing at all for my labels. 

0 Kudos
KellyHutchins
Esri Frequent Contributor

Tracy Schloss​ I don't think we support multi line labels. Can you link to the doc that says we do?

0 Kudos
TracySchloss
Frequent Contributor

It's in the labelClass API reference under the Property Details for labelExpression.

LabelClass | API Reference | ArcGIS API for JavaScript

At first I thought it was one of those instances that  it was supported in REST only but not in the JS API,  The API documentation references Labeling objects  ArcGIS REST API  so that example is shown in both places.

If it's not, I guess this is something else to clarify in the help.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Tracy Schloss​ looks like a documentation bug - we'll get it fixed. Our labelling logic only supports a combination of attribute fields and simple text. Honestly our Label documentation is a bit confusing right now because a few things changed in recent releases that didn't make it into the doc in a clear way. So we are working on cleaning up the doc for labeling and hope its all much clearer at the next release.

0 Kudos