Give label to KML polygon

I have been trying to figure out how to add a label to a polygon from a KML file, but I'm having no luck. Since the polygon doesn't have a position attribute, I'm unsure how to place the labels, since I want them approximately in the center of the polygons.

You have to specify both a Polygon and a Point inside a MultiGeometry.

<?xml version="1.0" encoding="UTF-8"?> > 0.0 ffffff00 normal 4400ff00 normal 1 1

A Polygon with label

#polygonStyle

0.0,0.0,0.0

10.0,10.0,0.0

10.0,-10.0,0.0

-10.0,-10.0,0.0

-10.0,10.0,0.0

10.0,10.0,0.0

``

Is the point supposed to be the "center" of the polygon? This is not ideal, as this would then have to be manual

I should also add I'm getting the label from the ExtendedData

Hi there,

Unfortunately manual is the way to go here, since polygons only take a list of positions in world space. The computation shouldn’t be bad though – you can do something simple like average positions.

Hope that helps,

  • Rachel

Even doing it manually doesn't center the label in the polygon, it is skewed towards where there are more coordinate points

That makes sense if you’re doing a simple average. To center, try computing the minimum and maximum coordinates and placing your label in the center (like a bounding box).

Placement of the label is a complicated problem. Think about where you would want to label a horseshoe or a polygon with interior holes. Also, in a dynamic viewer you might need to re-position the label if it is off-screen but part of the polygon is not.

Check out Mapbox’s polylabel library: https://github.com/mapbox/polylabel I’ve had some success in using it with Cesium to generate ideal label positions inside of a polygon. It’s mean for 2D so large polygons sometimes don’t have the ideal solution, but I find that it’s much more reliable than coming up with your own algorithm.