Bỏ qua

Thêm mẫu vào một đa giác

Thêm một mẫu vào một đa giác để tạo kiểu.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add a pattern to a polygon</title>
    <meta property="og:description" content="Use fill-pattern to draw a polygon from a repeating image pattern." />
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/trackasia-gl.css" />
    <script src="https://unpkg.com/[email protected]/dist/trackasia-gl.js"></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>

<script>
    const map = new trackasiagl.Map({
        container: 'map',
        style: 'https://maps.track-asia.com/styles/v2/streets.json?key=public_key',
        center: [106.660172, 10.762622],
        zoom: 9
    });

    map.on('load', async () => {
        // Add GeoJSON data
        map.addSource('source', {
            'type': 'geojson',
            'data': {
                'type': 'Feature',
                'properties': {},
                'geometry': {
                    'type': 'Polygon',
                    'coordinates': [
                        [
                            [106.50, 10.60],
                            [106.50, 10.90],
                            [106.85, 10.90],
                            [106.85, 10.60],
                            [106.50, 10.60]
                        ]
                    ]
                }
            }
        });

        // Load an image to use as the pattern
        try {
            const image = await map.loadImage('https://img.icons8.com/ios-filled/64/000000/cat.png');
            map.addImage('pattern', image.data);

            map.addLayer({
                'id': 'pattern-layer',
                'type': 'fill',
                'source': 'source',
                'paint': {
                    'fill-pattern': 'pattern'
                }
            });
        } catch (err) {
            console.error('Image pattern load failed.', err);

            // Show error text if image fails
            map.addSource('error-text', {
                'type': 'geojson',
                'data': {
                    'type': 'Feature',
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [106.660172, 10.762622]
                    }
                }
            });

            map.addLayer({
                'id': 'error-layer',
                'type': 'symbol',
                'source': 'error-text',
                'layout': {
                    'text-field': 'Error: Could not load the pattern image',
                    'text-size': 20,
                    'text-font': ['Noto Sans Regular']
                },
                'paint': {
                    'text-color': 'red'
                }
            });
        }
    });
</script>

</body>
</html>