8

To get the undesired result below, I:

  1. Uploaded a zipped shapefile to MapBox Studio.
  2. Using the shape, created a layer of polygons to display area boundaries.
  3. Using the same shape, created a text layer to label areas with their names.

Labels appear on the lines:

enter image description here

How can I make labels appear in the centre of the polygons?

1
  • 1
    I was looking for the same feature, and I find this completely lacking in MB. How ridiculous to have to label shapefiles separately...
    – ecuated
    Commented Jun 9, 2016 at 19:35

2 Answers 2

6

The feature of calculating label points for polygons on the client is currently in progress and will appear in one of the next releases. See:

4
  • Mourner, has this been released as a Mapbox Studio feature?
    – David
    Commented Sep 17, 2017 at 21:51
  • @David sadly it seems not (even many years later). That pull request appears to have been abandoned (after lengthy discussions), and the issue persists.
    – Mike Honey
    Commented Jun 7, 2020 at 0:55
  • @MikeHoney that's not true. The polylabel algorithms has been present in GL JS for years now, used for placing symbols for polygon features when symbol-placement is set to "point".
    – Mourner
    Commented Jul 8, 2020 at 13:48
  • Great! So how can we use them in Mapbox?
    – Mike Honey
    Commented Jul 11, 2020 at 13:50
4

Because of how vector-tiles work, you'll actually have to calculate the label locations yourself as a separate point layer. This should be pretty easy to do w/ any GIS program like qGIS/ArcGIS/PostGIS by calculating the centroid of each polygon, doing a spatial join to save the polygon label to the corresponding centroid point, and saving the result as a new point shp file.

Upload the result to MB, import it into your project and add labels via the new point layer, rather than the polygon layer.

The reason this is necessary is b/c vector-tiles actually split your polygons into tiles. Sufficiently large polygons will actually be cut into multiple polygons, so calculating polygon centroids on tiled polygons doesn't really make sense.

(by the way, I'm assuming you're using the new web-based MapBox Studio, not MapBox Studio Classic -- the answer is essentially the same either way, except that with the latter, you'd import the new label-point layer into your local download of MB Studio Classic, rather than uploading to your MB account).

5
  • With ArcGIS you simply turn on the 'display label' option and the system places labels in the centre of polygons (by default, but you can manipulate that). When you're suggesting me to do all the above, does this mean MB doesn't have a button (that I completely missed) that simply labels the polygon layer? You see what I mean? My initial approach was to create 2 layers, one to draw the polygons and another to display labels. Isn't there an option of a polygon layer in MB that I can simply turn on to display labels of that layer without having to create an extra layer just for the labels?
    – Rodrigo
    Commented Mar 19, 2016 at 17:59
  • 1
    No, there is not an option to turn on labels for polygons. This is b/c (as mentioned) MB Studio only has access to the tiled geometries, which prevents it from being able to calculate centroids for label placement. ArcGIS/typical GIS software, on the other hand, reads non-tiled geometries, which allows it to calculate centroids on the fly. (incidentally, reading tiled geometries is what enables MB Studio to consume high-precision global datasets like roads/boundaries/terrain/etc--so, some things are lost w/ tiling, some things gained...) Commented Mar 19, 2016 at 21:32
  • ...that being said, if (judging from you pic) you are adding labels to administrative polygons, MB has already generated many useful tile layers for stuff like this. Check out mapbox-streets-v7 #admin layer and #state_label. Should be easy to pull these into your project, and then you don't need to maintain your own versions of the data. Commented Mar 19, 2016 at 21:37
  • "incidentally, reading tiled geometries is what enables MB Studio to consume high-precision global datasets like roads/boundaries/terrain/etc" Don't over-read the hype on vector tiles. High precision global datasets are easily handled in a variety of systems without vector tiles. That said, vector tiles solve a lot of problems for distribution and device rendering. Commented Aug 3, 2016 at 21:45
  • > "High precision global datasets are easily handled in a variety of systems" Yes, but not in the browser. Commented Aug 3, 2016 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.