Add a pattern to a polygon

Use the fill-pattern feature to draw a polygon by employing a repeating image pattern. This technique allows for the creation of visually appealing shapes by using a repeated image as a fill. To implement this, refer to the layer ⇾ fill ⇾ fill-pattern section in the GL Style Specification.

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.STREETS,
    center: [0, 0],
    zoom: 1
});

map.on('load', async function () {
    // Add GeoJSON data
    map.addSource('source', {
        'type': 'geojson',
        'data': {
            'type': 'Feature',
            'properties': {},
            'geometry': {
                'type': 'Polygon',
                'coordinates': [
                    [
                        [-30, -25],
                        [-30, 35],
                        [30, 35],
                        [30, -25],
                        [-30, -25]
                    ]
                ]
            }
        }
    });

    // Load an image to use as the pattern
    const image = await map.loadImage(
        'https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/64px-Cat_silhouette.svg.png');

    // Declare the image
    map.addImage('pattern', image.data);

    // Use it
    map.addLayer({
        'id': 'pattern-layer',
        'type': 'fill',
        'source': 'source',
        'paint': {
            'fill-pattern': 'pattern'
        }
    });
});
Show polygon data from GeoJSON on the map

GeoJSON polygon layer

Tutorials

Add GeoJSON polygon overlay to your web map application.

Polygon fill pattern (polygon helper)

Polygon fill pattern (polygon helper)

Examples

Add fill pattern to polygon layer using helper.

Polygon color ramp symbol (polygon helper)

Polygon color ramp symbol (polygon helper)

Examples

Style polygon fill color based on zoom level.

Show polygon information on click

Show polygon information on click

Examples

Display a popup when a user clicks a polygon.

Was this helpful?