Change a layer’s color with buttons

Use setPaintProperty to change a layer’s fill color.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
    container: 'map',
    style: MapStyle.BASE,
    center: [12.338, 45.4385],
    zoom: 17.4
});

const swatches = document.getElementById('swatches');
const layer = document.getElementById('layer');
const colors = [
    '#ffffcc',
    '#a1dab4',
    '#41b6c4',
    '#2c7fb8',
    '#253494',
    '#fed976',
    '#feb24c',
    '#fd8d3c',
    '#f03b20',
    '#bd0026'
];

colors.forEach(function (color) {
    const swatch = document.createElement('button');
    swatch.style.backgroundColor = color;
    swatch.addEventListener('click', function () {
        map.setPaintProperty(layer.value, 'fill-color', color);
    });
    swatches.appendChild(swatch);
});
Change building color based on zoom level

Change color based on zoom level

Examples

Dynamically adjust 3D building colors based on zoom levels.

Style lines with a data-driven property

Style lines with a data-driven property

Examples

Create visualizations using data expressions for line colors.

Custom color ramp (color ramp)

Custom color ramp (color ramp)

Examples

Visualize point layers by creating custom map color ramps.

Point layer colored and sized according to a property (point helper)

Point layer colored and sized according to a property (point helper)

Examples

Style point layer by property using point helper.

Was this helpful?