Last active
March 4, 2022 05:19
-
-
Save malwoodsantoro/9e92c468984fa1fbbf0469d82342f2eb to your computer and use it in GitHub Desktop.
Use expressions for icon-size and icon-color (SDFs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>Display a map</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.47.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.47.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.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', // container id | |
style: 'mapbox://styles/mal-wood/cjkc0b5nh3br32rktj680mixa', // stylesheet location | |
center: [-70.251397, 43.655894], // starting position [lng, lat] | |
zoom: 15.3 // starting zoom | |
}); | |
map.on('load', function () { | |
map.loadImage('https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/FireIcon.svg/313px-FireIcon.svg.png', function(error, image) { | |
if (error) throw error; | |
map.addImage('fire', image, {sdf: true}); | |
}); | |
map.loadImage('http://cdn.onlinewebfonts.com/svg/img_146938.png', function(error, image) { | |
if (error) throw error; | |
map.addImage('child', image, {sdf: true}); | |
}); | |
map.loadImage('https://png.icons8.com/metro/1600/car.png', function(error, image) { | |
if (error) throw error; | |
map.addImage('car', image, {sdf: true}); | |
}); | |
map.loadImage('http://cdn.onlinewebfonts.com/svg/img_550705.png', function(error, image) { | |
if (error) throw error; | |
map.addImage('robber', image, {sdf: true}); | |
}); | |
map.loadImage('https://png.icons8.com/metro/1600/home.png', function(error, image) { | |
if (error) throw error; | |
map.addImage('house', image, {sdf: true}); | |
}); | |
map.addLayer({ | |
"id": "crime-images", | |
"type": "symbol", | |
"source": { | |
type: 'vector', | |
url: 'mapbox://mal-wood.cjkczcuuj00mqx0mr0tmgyilg-945mu' | |
}, | |
"source-layer": "crime-demo", | |
"layout": { | |
"icon-image": [ | |
"match", | |
["string", ["get", "crime-type"]], | |
"arson", | |
"fire", | |
"kidnapping", | |
"child", | |
"home-invasion", | |
"house", | |
"robbery", | |
"robber", | |
"assault", | |
"car", | |
"robber" | |
], | |
"icon-size": .12, | |
//if you used icons that were the same size, you could use an expression here like ["*", .25, ["get", "police-units"] | |
}, | |
"paint": { | |
"icon-color": [ | |
"match", | |
["string", ["get", "crime-type"]], | |
"arson", | |
"#ff0000", | |
"kidnapping", | |
"#228B22", | |
"home-invasion", | |
"#008B8B", | |
"robbery", | |
"#BDB76B", | |
"assault", | |
"#FF4500", | |
"#FF4500" | |
] | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment