Select to view content in your preferred language

Place 2D polygon labels inside polygon

1997
8
08-21-2018 12:09 PM
DanFrey
Occasional Contributor

I'm about to throw in the towel on this one, but figure I'd ask the question to make sure I'm not missing anything. I've got my JSAPI 4.8 2D WebGL polygon labels working great on a FeatureLayer - fonts, halos, scale sensitivity. What I realize now though is that labels are placed at polygon centroids. Many of my polygons, political districts in this case, are irregularly shaped, so the center falls outside of the polygon. The reason for labeling the polygons is so that users can identify them. I'm afraid the net effect in this case is that districts will be labeled 'wrong', thereby sowing confusion. There would be no way to force the labels back into their respective polygons, right???

Tags (1)
8 Replies
RobertScheitlin__GISP
MVP Emeritus

Dan, I do not know of anyway to do this in the client side currently. If you can use a MapImageLayer then you can have ArcMap label the polygons for you in your MapService.

0 Kudos
OrenGal
Regular Contributor

Hi,

I'm using this https://github.com/GeorgeGardiner/LabelPoint

Works well.

Oren

OrenGal
Regular Contributor

BTW a live sample : https://gisn.tel-aviv.gov.il/iView2js/beta4/index.aspx 

Measure area and the value will be displayed at the best location.

0 Kudos
DanFrey
Occasional Contributor

The MapImageLayer suggestion is a good one, Robert. That's got me thinking, and reviewing possible layer types, which is a good thing. Since I'm using AGOL hosting, I think that particular one is not an option, but some manner server-side rendering might be the best way to go.

Your proposed solution, Oren, has got my interest too. I'm a bit concerned about too many calls to that function, with too many vertices, with every scale and extent change ideally, but it may work just fine. I think I'll have to dig into it a bit more. 

Alternative #3, I suppose I could create my own label point services. They wouldn't be dynamic, but at least they'd be predictable. 

One way or another, I think this is going to work. Thanks both for your input.

0 Kudos
OrenGal
Regular Contributor

Hello Dan,

Computers are fast, I would not worry about the recalculation...   

Still, You can calc one time the x,y of label point and put it in the layer. Then you don't need to recalculate.

Another idea is to check if cetroid or center falls inside the polygon and only if not then calc label point. 

0 Kudos
Justin_Greco
Frequent Contributor

Are there any plans to improve the labeling of polygons?  This is not an issue at 3.x, the labels readjust as the extent changes.  I know the recommendation is to use map image layers, but thats not an acceptable answer when using ArcGIS Online layers.  I have looked clipping the features to the map extent and then labeling the centroid, but have run into some issues determining how to get the label expression to work.

0 Kudos
JohnGrayson
Esri Regular Contributor
0 Kudos
DanMcCoy1
Occasional Contributor

There is a bug logged for this
BUG-000145531 - Labels fall outside the polygons when the polygons contain centroids outside the polygon boundary in ArcGIS Online Map Viewer.

Bug ID Number BUG-000145531
SubmittedDecember 23, 2021
Last ModifiedJuly 17, 2023
Applies toArcGIS Online
Version found9.3
Operating SystemN/A
Operating System VersionN/A
StatusKnown Limit 
 

Additional Information

This is a known limitation for the placement of the labels in ArcGIS API for JavaScript 4.x, where labels are placed at the shape centroid and not within the shape boundary. This is on the roadmap in the API to improve the label placement.



0 Kudos