Skip to content

Instantly share code, notes, and snippets.

@dianaow
Created September 12, 2020 04:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dianaow/f6b6166c313b4675469732ec19252cbf to your computer and use it in GitHub Desktop.
Save dianaow/f6b6166c313b4675469732ec19252cbf to your computer and use it in GitHub Desktop.
Using deck.gl with Mapbox GL JS (Method 1): Deck canvas overlay
<html>
<head>
<title>deck.gl ScatterplotLayer Example</title>
<script src="https://d3js.org/d3.v5.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/deck.gl@latest/dist.min.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
<style type="text/css">
body {
width: 100vw;
height: 100vh;
margin: 0;
position: relative;
}
.panel {
background-color: white;
opacity: 0.7;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
#tooltip {
position: absolute;
top: 0;
left: 0;
z-index: 2;
background-color: white;
visibility: visible;
padding: 5px;
}
</style>
</head>
<body>
<div class='panel'>
<input type="radio" id="satellite" name="map_style" value="satellite">
<label for="satellite">Satellite</label><br>
<input type="radio" id="street" name="map_style" value="street">
<label for="street">Street</label><br>
<input type="radio" id="dark" name="map_style" value="dark">
<label for="dark">Dark</label><br>
</div>
<div id='map'></div>
<div id='tooltip'><div>
</body>
<script type="text/javascript">
const {DeckGL, ScatterplotLayer, HeatmapLayer} = deck;
d3.json("data.json").then(function(data){
function initHeatmapLayer(){
return new HeatmapLayer({
data,
id: 'heatmap-layer',
getPosition: d => {
//console.log([+d.lat, +d.lon])
return [+d.lon, +d.lat] },
getWeight: 1,
colorRange: [[1, 152, 189, 255], [73, 227, 206, 255], [216, 254, 181, 255], [254, 237, 177, 255],[254, 173, 84, 255],[209, 55, 78, 255]],
radiusPixels: 20,
intensity: 1.2,
threshold: 0.03
})
}
function initScatterLayer(){
return new ScatterplotLayer({
id: 'scatter-plot',
data,
getPosition: d => [+d.lon, +d.lat],
getFillColor: [1, 152, 189, 255],
//getRadius: 500,
radiusScale: 1,
radiusMaxPixels: 10,
radiusMinPixels: 5,
opacity: 0.5,
pickable: true,
//onHover: info => showTooltip(info),
onClick: info => showTooltip(info)
})
}
const token = 'pk.eyJ1IjoiZGlhbmFtZW93IiwiYSI6ImNqcmh4aWJnOTIxemI0NXA0MHYydGwzdm0ifQ.9HakB25m0HLT-uDY2yat7A'
const deckgl = new DeckGL({
mapboxApiAccessToken: token,
mapStyle: 'mapbox://styles/mapbox/dark-v9',
initialViewState: {
container: 'map',
longitude: 103.84,
latitude: 1.35,
zoom: 11,
pitch: 0,
bearing: 0,
renderWorldCopies: 1
},
controller: true,
layers: [
initHeatmapLayer()
],
onViewStateChange: ({viewState}) => {
d3.select('#tooltip').style('visibility', 'hidden')
if(viewState.zoom > 14){
deckgl.setProps({layers: [initScatterLayer()]});
} else {
deckgl.setProps({layers: [initHeatmapLayer()]});
}
},
// built-in tooltips show on hover
//getTooltip: ({object}) => object && {
//html: `<h2>${object[2]}</h2>`,
//style: {
//backgroundColor: '#fff',
//fontSize: '0.8em'
//}
//}
});
function showTooltip(info){
d3.select('#tooltip')
.style('top', info.y)
.style('left', info.x)
.style('visibility', 'visible')
.html(`<p>${info.object.town}</p><p>${info.object.address}</p>`)
}
document.querySelector("#satellite").addEventListener('click',function (){
deckgl.setProps({mapboxApiAccessToken: token, mapStyle: 'mapbox://styles/mapbox/satellite-v9'});
})
document.querySelector("#street").addEventListener('click',function (){
deckgl.setProps({mapboxApiAccessToken: token, mapStyle: 'mapbox://styles/mapbox/streets-v11'});
})
document.querySelector("#dark").addEventListener('click',function (){
deckgl.setProps({mapboxApiAccessToken: token, mapStyle: 'mapbox://styles/mapbox/dark-v9'});
})
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment