Bỏ qua

Sử dụng hình ảnh dự phòng

Sử dụng biểu thức coalesce để hiển thị một hình ảnh khác khi hình ảnh được yêu cầu không khả dụng.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Use a fallback image</title>
    <meta property="og:description" content="Use a coalesce expression to display another image when a requested image is not available." />
    <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/v1/streets.json?key=public_key',
            center: [108.223071,16.109343],
            zoom: 12
        });
        map.on('load', () => {
            map.addSource('points', {
                'type': 'geojson',
                'data': {
                    'type': 'FeatureCollection',
                    'features': [
                        {
                            'type': 'Feature',
                            'geometry': {
                                'type': 'Point',
                                'coordinates': [108.226592,16.106869]
                            },
                            'properties': {
                                'title': 'Danang City',
                                'icon': 'not-exists'
                            }
                        },
                        {
                            'type': 'Feature',
                            'geometry': {
                                'type': 'Point',
                                'coordinates': [108.220923,16.095242]
                            },
                            'properties': {
                                'title': 'Thuan Phuoc Bridge',
                                'icon': 'bridge'
                            }
                        },
                        {
                            'type': 'Feature',
                            'geometry': {
                                'type': 'Point',
                                'coordinates': [108.216494,16.119815]
                            },
                            'properties': {
                                'title': 'Tien Sa Port',
                                'icon': 'harbor'
                            }
                        }
                    ]
                }
            });
            map.addLayer({
                'id': 'points',
                'type': 'symbol',
                'source': 'points',
                'layout': {
                    'icon-image': [
                        'coalesce',
                        ['image', ['concat', ['get', 'icon'], '-11']],
                        ['image', 'marker-11']
                    ],
                    'text-field': ['get', 'title'],
                    'text-font': ['Open Sans Semibold'],
                    'text-offset': [0, 0.6],
                    'text-anchor': 'top'
                }
            });
        });
    </script>

</body>
</html>