{
let container = html`<div style='height:800px; .custom' />`;
let hexagons = null;
let zoom = 2;
let hoveredHexId = null;
const colors = asMapboxStepExpr(colorScaleAuto);
yield container;
let map = new mapboxgl.Map({
container,
zoom: zoom,
style: "mapbox://styles/franckalbinet/ckyaawysn1qlg15ny2auxlk8k",
projection: {
name: projMapbox
}
});
// .setLngLat([-1.608943147966467, 43.425281054110535])
// .addTo(map);
const marker = new mapboxgl.Marker({
draggable: true,
anchor: "center",
className: "custom-icon"
// color: "steelblue"
})
.setLngLat([-1.608943147966467, 43.425281054110535])
.addTo(map);
// new mapboxgl.Marker(el)
// .setLngLat(marker.geometry.coordinates)
// .addTo(map);
// Create a popup, but don't add it to the map yet.
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.addControl(new mapboxgl.NavigationControl(), "top-right");
map.on("boxzoomend", (e) => {
// let coords = map.unproject([
// e.originalEvent.clientX,
// e.originalEvent.clientY
// ]);
// debugger;
// map.boxZoom.disable();
// e.stopPropagation();
map.fitBounds(
[
[-1.59, 43.43], // [lng, lat] - southwestern corner of the bounds
[-1.58, 43.44] // [lng, lat] - northeastern corner of the bounds
],
{ center: [-1.585, 43.435] }
);
e.preventDefault();
// console.log("event type:", e.type);
});
map.on("load", () => {
https: map.addSource("hexbins", {
type: "geojson",
data: getHexagons(
dataRdnDepth,
zoomScale(map.getZoom()),
map.getBounds()
),
generateId: true
});
// Hexbins fill
map.addLayer({
id: "hexbins-fill",
type: "fill",
source: "hexbins",
layout: {},
paint: {
"fill-color": ["step", ["get", "max", ["get", "value"]], ...colors],
"fill-opacity": 0.8
}
});
map.addLayer({
id: "hexbins-line",
type: "line",
source: "hexbins",
layout: {},
paint: {
"line-color": [
"case",
["boolean", ["feature-state", "hover"], false],
"black",
"#bbb"
],
"line-opacity": 0.5,
"line-width": [
"case",
["boolean", ["feature-state", "hover"], false],
4,
0.5
]
}
});
});
map.on("zoomend", () => {
// console.log(map.getZoom());
map
.getSource("hexbins")
.setData(
getHexagons(dataRdnDepth, zoomScale(map.getZoom()), map.getBounds())
);
});
map.on("moveend", () => {
map
.getSource("hexbins")
.setData(
getHexagons(dataRdnDepth, zoomScale(map.getZoom()), map.getBounds())
);
});
// When the user moves their mouse over the state-fill layer, we'll update the
// feature state for the feature under the mouse.
map.on("mousemove", "hexbins-fill", (e) => {
map.getCanvas().style.cursor = "pointer";
if (e.features.length > 0) {
if (hoveredHexId !== null) {
map.setFeatureState(
{ source: "hexbins", id: hoveredHexId },
{ hover: false }
);
}
hoveredHexId = e.features[0].id;
map.setFeatureState(
{ source: "hexbins", id: hoveredHexId },
{ hover: true }
);
}
// Copy coordinates array.
const coordinates = turf.centroid(turf.centroid(e.features[0].geometry))
.geometry.coordinates;
const description = JSON.parse(e.features[0].properties.value).max;
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates).setHTML(description).addTo(map);
});
// When the mouse leaves the state-fill layer, update the feature state of the
// previously hovered feature.
map.on("mouseleave", "hexbins-fill", () => {
map.getCanvas().style.cursor = "";
if (hoveredHexId !== null) {
map.setFeatureState(
{ source: "hexbins", id: hoveredHexId },
{ hover: false }
);
}
hoveredHexId = null;
popup.remove();
});
map.addControl(new mapboxgl.FullscreenControl());
// Be careful to clean up the map's resources using \`map.remove()\` whenever
// this cell is re-evaluated.
invalidation.then(() => map.remove());
}