TrackAsiaTrackAsia GL JS DocsExamplesStyle lines with a data-driven property

Style lines with a data-driven property

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

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Style lines with a data-driven property</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.js"></script>
<link href="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new trackasiagl.Map({
container: 'map',
style: 'https://maps.track-asia.com/styles/v1/streets.json?key=public_key',
center: {"lat":10.762622,"lng":106.660172},
zoom: 16
});
map.on('load', function () {
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>