Bỏ qua

Thêm một đường GeoJSON

Thêm một đường GeoJSON vào bản đồ bằng addSource, sau đó tạo kiểu cho nó bằng các thuộc tính paint của addLayer.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add a GeoJSON line</title>
    <meta property="og:description" content="Add a GeoJSON line to a map using addSource, then style it using addLayer’s paint properties." />
    <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.720772,10.77423],
        zoom: 14
    });

    map.on('load', () => {
        map.addSource('route', {
            'type': 'geojson',
            'data': {
                'type': 'Feature',
                'properties': {},
                'geometry': {
                    'type': 'LineString',
                    'coordinates': [[106.716666,10.771724],[106.716387,10.773832],[106.717031,10.775835],[106.714176,10.777268],[106.715164,10.778638],[106.716087,10.777964],[106.717075,10.77887],[106.718341,10.777247],[106.722206,10.778491],[106.72255,10.776657],[106.722271,10.774528],[106.72255,10.773463],[106.732306,10.778378]]
                }
            }
        });
        map.addLayer({
            'id': 'route',
            'type': 'line',
            'source': 'route',
            'layout': {
                'line-join': 'round',
                'line-cap': 'round'
            },
            'paint': {
                'line-color': 'red',
                'line-width': 6
            }
        });
    });
</script>

</body>
</html>