Skip to content

Style lines with a data-driven property

Create a visualization with a data expression for line-color.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Style lines with a data-driven property</title>
    <meta property="og:description" content="Create a visualization with a data expression for line-color." />
    <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: {"lat":10.762622,"lng":106.660172},
        zoom: 16
    });

    map.on('load', () => {
        map.addSource('lines', {
            'type': 'geojson',
            'data': {
                'type': 'FeatureCollection',
                'features': [
                    {
                        'type': 'Feature',
                        'properties': {
                            'color': '#F7455D' // red
                        },
                        'geometry': {
                            'type': 'LineString',
                            'coordinates': [[106.658398,10.764238],[106.658967,10.764311],[106.659041,10.764573],[106.659674,10.764642],[106.659883,10.763972],[106.65984,10.76383],[106.657645,10.762629],[106.657092,10.763493]]
                        }
                    },
                    {
                        'type': 'Feature',
                        'properties': {
                            'color': '#33C9EB' // blue
                        },
                        'geometry': {
                            'type': 'LineString',
                            'coordinates': [[106.66015,10.764536],[106.660312,10.764083],[106.659914,10.763872],[106.660216,10.762903],[106.663366,10.763609],[106.663387,10.763503],[106.663704,10.763356],[106.663769,10.763277],[106.663801,10.763198],[106.663806,10.763124],[106.663806,10.763071],[106.663785,10.763029],[106.66372,10.762903],[106.66386,10.762824]]
                        }
                    }
                ]
            }
        });
        map.addLayer({
            'id': 'lines',
            'type': 'line',
            'source': 'lines',
            'paint': {
                'line-width': 3,
                // Use a get expression (https://docs.track-asia.com/style/expressions/#get)
                // to set the line-color to a feature property value.
                'line-color': ['get', 'color']
            }
        });
    });
</script>

</body>
</html>