viewof map = {
const container = html`<div id="map">`;
yield container;
const map = (container.value = new mapboxgl.Map({
container,
center: [-80.0721, 50.711],
zoom: 1.5,
style: "mapbox://styles/mapbox/light-v10",
scrollZoom: true
}));
invalidation.then(() => map.remove());
const southWest = new mapboxgl.LngLat(-170, 14.6);
const northEast = new mapboxgl.LngLat(-50, 72);
const boundingBox = new mapboxgl.LngLatBounds(southWest, northEast);
const bb = container.getBoundingClientRect();
var svg = d3
.select(container)
.append("svg")
.style("position", "absolute")
.style("top", 0)
.style("left", 0)
.attr("width", bb.width)
.attr("height", bb.height)
.style("pointer-events", "none");
//Project any point to map's current state
function projectPoint(lon, lat) {
var point = map.project(new mapboxgl.LngLat(lon, lat));
this.stream.point(point.x, point.y);
}
function project(d) {
return map.project(new mapboxgl.LngLat(d[0], d[1]));
}
//Projection function
var transform = d3.geoTransform({ point: projectPoint });
var path = d3.geoPath().projection(transform);
//Draw geojson data with d3
var lines;
var points;
var tooltip = d3.select('body')
.append('div')
.attr('class', 'hidden tooltip');
function drawData(err) {
lines = svg.append("g")
.selectAll("path")
.data(smokeGeojson.features)
.enter()
.append("path")
.attr("class", function(d) {return d.properties["styleUrl"];})
.attr("fill", function(d) {return d.properties["fill"];})
// .attr("fill-opacity", function(d) {return d.properties["fill-opacity"];})
.attr("fill-opacity", 0.15)
.attr("stroke", function(d) {return d.properties["stroke"];})
.attr("stroke-opacity", function(d) {return d.properties["stroke-opacity"];})
.attr("d", path)
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.routes_r_1);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);;
});
points = svg.append("g")
.selectAll("circles")
.data(fireGeojson.features)
.enter()
.append("circle")
.attr("class", "fire")
.attr("r", 2)
.style("fill", "#ff0000")
.attr("fill-opacity", 0.05)
.attr("cx", function (d) {
return project(d.geometry.coordinates).x;
})
.attr("cy", function (d) {
return project(d.geometry.coordinates).y;
})
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.routes_r_1);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);;
});
;
map.on("viewreset", update);
map.on("move", update);
map.on("moveend", update);
}
function render() {
points
.attr("cx", function (d) {
return project(d.geometry.coordinates).x;
})
.attr("cy", function (d) {
return project(d.geometry.coordinates).y;
});
}
//update D3 shapes' positions to the map's current state
function update() {
console.log("update");
lines.attr("d", path);
render();
}
drawData();
update(500);
}