Skip to content

Instantly share code, notes, and snippets.

@danswick
Created July 14, 2017 00:48
Show Gist options
  • Save danswick/6e5fd0262f4ee70a490e67d1084494b5 to your computer and use it in GitHub Desktop.
Save danswick/6e5fd0262f4ee70a490e67d1084494b5 to your computer and use it in GitHub Desktop.
Use data-driven styles with Mapbox GL JS to offset three overlapping line features
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.38.0/mapbox-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>
mapboxgl.accessToken = 'pk.eyJ1IjoiZGFuc3dpY2siLCJhIjoiY2l1dTUzcmgxMDJ0djJ0b2VhY2sxNXBiMyJ9.25Qs4HNEkHubd4_Awbd8Og';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 17,
center: [-89.401975,43.075151]
});
map.on('load', function () {
map.addLayer({
'id': 'route',
'type': 'line',
'source': {
type: 'geojson',
data: geojson
},
'paint': {
// data-driven style for line color
'line-color': {
property: 'color',
type: 'identity'
},
//data-driven style for line offset
'line-offset': {
property: 'Order',
type: 'categorical',
stops: [
[1, 0],
[2, 3],
[3, 6]
]
},
'line-width': 3
}
});
});
// Line geometry
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"Order": 1,
"color": "#ef4747"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-89.4034069776535,
43.075526006595695
],
[
-89.40307974815369,
43.07548290350965
],
[
-89.40317094326019,
43.07501660454956
],
[
-89.40240383148193,
43.074934316129465
],
[
-89.40231800079346,
43.07540061571569
],
[
-89.40167963504791,
43.07532616475928
],
[
-89.40176010131836,
43.074855946102815
],
[
-89.4010466337204,
43.074765820448306
],
[
-89.40092861652374,
43.0748441905902
],
[
-89.40085887908936,
43.074965664112035
],
[
-89.40087497234344,
43.075141996215336
],
[
-89.40094470977783,
43.07522036587615
],
[
-89.40101981163025,
43.075259550668974
],
[
-89.4014060497284,
43.075306572387284
]
]
}
},
{
"type": "Feature",
"properties": {
"Order": 2,
"color": "#ffea51"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-89.4034069776535,
43.075526006595695
],
[
-89.40307974815369,
43.07548290350965
],
[
-89.40317094326019,
43.07501660454956
],
[
-89.40240383148193,
43.074934316129465
],
[
-89.40231800079346,
43.07540061571569
],
[
-89.40167963504791,
43.07532616475928
],
[
-89.40176010131836,
43.074855946102815
],
[
-89.4010466337204,
43.074765820448306
],
[
-89.40092861652374,
43.0748441905902
],
[
-89.40085887908936,
43.074965664112035
],
[
-89.40087497234344,
43.075141996215336
],
[
-89.40094470977783,
43.07522036587615
],
[
-89.40101981163025,
43.075259550668974
],
[
-89.4014060497284,
43.075306572387284
]
]
}
},
{
"type": "Feature",
"properties": {
"Order": 3,
"color": "#7fba41"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
-89.4034069776535,
43.075526006595695
],
[
-89.40307974815369,
43.07548290350965
],
[
-89.40317094326019,
43.07501660454956
],
[
-89.40240383148193,
43.074934316129465
],
[
-89.40231800079346,
43.07540061571569
],
[
-89.40167963504791,
43.07532616475928
],
[
-89.40176010131836,
43.074855946102815
],
[
-89.4010466337204,
43.074765820448306
],
[
-89.40092861652374,
43.0748441905902
],
[
-89.40085887908936,
43.074965664112035
],
[
-89.40087497234344,
43.075141996215336
],
[
-89.40094470977783,
43.07522036587615
],
[
-89.40101981163025,
43.075259550668974
],
[
-89.4014060497284,
43.075306572387284
]
]
}
}
]
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment