Select to view content in your preferred language

Wrap/stack a text string label in an AGOL web map

5383
20
Jump to solution
02-01-2022 12:23 PM
JayHodny
Frequent Contributor

Hello.  I have labels using an attribute text string that I would like to "wrap and stack" on my web map.  In Pro, I am able to do this using Labeling Properties (see screen shot).  I published the service but the labeling itself did not carry through to the service (maybe I missed a setting in the Share Web Layer?).

JayHodny_0-1643746462967.png

I read that classic map viewer does not support the idea but the new map viewer does (maybe)?  I opened my web map in the new map viewer and poked around on the Label features dialogue but was not successful.

JayHodny_1-1643746906637.png

I am an Arcade beginner, but was wondering if there is an arcade function or code snippet available to do this, for example, wrap and stack the text string at the first space in the string?  Thanks!

 

20 Replies
sajjady
Emerging Contributor

Hi @KenBuja  Its really work but this expression is for only one field. I have three fields to lable for a polygon. Could you plese share your eperience how i can lable 3 fields with stack/wrap.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

You can always add more fields to a label string. This example uses template literals.

var output = `${$feature.field1} and ${$feature.field2}`

 

0 Kudos
JonathanHallam1
Regular Contributor

Used the top code but doens't work when field has a list with Domain

$feature["Camper"]
DomainName($feature, "Camper")

How do I update code?
0 Kudos
MappyIan
Frequent Contributor

Thanks for these bits of code @KenBuja, but I had a problem with the first example if the feature has a <null> value for the field I was trying to label with.  I modified the code to add an if statement so that it only tries to add a new line if there is a value in the field:

var array = split($feature.yourField, " ");
var string;
if (Count(array)>1) {
  for (var k in array){
    if (count(array[k])>2){
      string += array[k] + TextFormatting.NewLine
    }
    else {
      string += array[k] +" ";
    }
  }
}
return string;

The code above also includes a check on the length of each value in the array and only adds a new line if the word is >2 characters in length (e.g. so that single/two letter words are not split on to separate lines).

Just sharing so others don't have to spend ages trying to figure it out if they hit the same problem.

0 Kudos
SarahHartholt
Frequent Contributor

Is it possible to create multiline labels in map viewer classic without using the split function?

I've launched the election solution which was created using map viewer classic. I would like to label my precincts by their number and name (i.e. Precinct 01 Cambell) with a line break after the number. My current arcade expression looks something like the following and returns a single line label:

var code = $feature.districtid;
if (code == '01'){
    return "Precinct 01" + TextFormatting.NewLine + "Cambell"
}
else if (code == '02'){....

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Map Viewer Classic is based on JavaScript 3.x and TextFormatting.NewLine does not work in that version. Adding in a line break only works with the new Map Viewer since it is based on JavaScript 4.x which doesn't have that limitation.

0 Kudos
Lea_DyrholmHansen
Emerging Contributor

Hi @KenBuja 

Do you know why stacking of labels doesn't work i Web AppBuilder eventhough it is set in ArcGIS Pro before publishing a feature service to ArcGIS Protal?

I have a long textfield that I for some features need stacking on.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Web AppBuilder is built on JavaScript 3.x, which cannot render line breaks in labels. You would have to use Experience Builder, which is built on JavaScript 4.x, to render the line breaks.

0 Kudos
Lea_DyrholmHansen
Emerging Contributor

Thanks, I will look forward to using Experience Builder. Our system is not ready for that yet. 

0 Kudos
GreenInfoNetwork
Emerging Contributor

@KenBuja Is it possible to add left or right justify to your code?

0 Kudos