Place 2D polygon labels inside polygon

729
7
08-21-2018 12:09 PM
DanFrey
New Contributor II

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)
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

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
New Contributor III

Hi,

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

Works well.

Oren

OrenGal
New Contributor III

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
New Contributor II

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
New Contributor III

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
JustinGreco1
New Contributor III

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