Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add background color for layer text field in mapbox-gl

Tags:

mapbox-gl-js

how to add a background color for layer text-field in mapbox-gl.. or how it can be done so that there's background box on the text-field

map.addLayer({
    "id": "markers",
    "type": "symbol",
    "source": "markers",
    "layout": {
        "icon-image": "{marker-symbol}-15",
        "text-field": "{title}",
        "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
        "text-offset": [0, 0.6],
        "text-anchor": "top"
    }
});
like image 929
Zulhalimi Anuar Muhammad Avatar asked Apr 21 '16 15:04

Zulhalimi Anuar Muhammad


People also ask

How do I change the color of my Mapbox?

Mapbox will open a design window where you can adjust your chosen style. On the left side you will find map elements, where you can change colors and other properties. If you don't want to adjust anything just click on 'Go back' arrow at the top left.

How do I get layered data in Mapbox?

In the Select data tab Open the style in the Mapbox Studio style editor. Click on the name of the layer in the layer list. Switch to the Select data tab. Find the source layer listed below the name of the tileset source.

What are layers in Mapbox?

The type of layer is specified by the "type" property, and must be one of background , fill , line , symbol , raster , circle , fill-extrusion , heatmap , hillshade , sky . Except for layers of the background or sky types, each layer must refer to a source.


1 Answers

While I also don't know how to draw a background box, we may be looking for the same thing which is to obscure any other text beneath the custom label so that it pops and is more legible. If so, I did discover that you can add a "halo" which will achieve the desired effect.

"layout": {
                "icon-image": symbol + "-15",
                "icon-allow-overlap": true,
                "text-field": symbol,
                "text-font": ["Open Sans Bold", "Arial Unicode MS Bold"],
                "text-size": 11,
                "text-transform": "uppercase",
                "text-letter-spacing": 0.05,
                "text-offset": [0, 1.5]
            },
            "paint": {
                "text-color": "#202",
                "text-halo-color": "#fff",
                "text-halo-width": 2
            },

This was taken from this example on their site.

like image 102
headwinds Avatar answered Sep 16 '22 11:09

headwinds