Allow double click zooms with setTimeout
Still not perfect, but good enough for now.
This commit is contained in:
parent
c417222e2c
commit
d6720da142
@ -274,89 +274,100 @@ const Map: React.FC = () => {
|
|||||||
map.current.addControl(new mapboxgl.NavigationControl());
|
map.current.addControl(new mapboxgl.NavigationControl());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let singleClickTimeout: NodeJS.Timeout | null = null;
|
||||||
map.current.on("click", "grid-layer", (e) => {
|
map.current.on("click", "grid-layer", (e) => {
|
||||||
if (e.features && e.features[0]) {
|
const features = e.features;
|
||||||
const cell: [number, number] = [
|
if (singleClickTimeout) return;
|
||||||
e.features[0].properties!.cellX,
|
singleClickTimeout = setTimeout(() => {
|
||||||
e.features[0].properties!.cellY,
|
singleClickTimeout = null;
|
||||||
];
|
if (features && features[0]) {
|
||||||
map.current.removeFeatureState({ source: "grid-source" });
|
const cell: [number, number] = [
|
||||||
map.current.setFeatureState(
|
features[0].properties!.cellX,
|
||||||
{
|
features[0].properties!.cellY,
|
||||||
source: "grid-source",
|
];
|
||||||
id: e.features[0].id,
|
map.current.removeFeatureState({ source: "grid-source" });
|
||||||
},
|
map.current.setFeatureState(
|
||||||
{
|
|
||||||
selected: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
setSelectedCell(cell);
|
|
||||||
map.current.resize();
|
|
||||||
|
|
||||||
var zoom = map.current.getZoom();
|
|
||||||
var viewportNW = map.current.project([-180, 85.051129]);
|
|
||||||
var cellSize = Math.pow(2, zoom + 2);
|
|
||||||
const x = e.features[0].properties!.x;
|
|
||||||
const y = e.features[0].properties!.y;
|
|
||||||
let nw = map.current.unproject([
|
|
||||||
x * cellSize + viewportNW.x,
|
|
||||||
y * cellSize + viewportNW.y,
|
|
||||||
]);
|
|
||||||
let ne = map.current.unproject([
|
|
||||||
x * cellSize + viewportNW.x + cellSize,
|
|
||||||
y * cellSize + viewportNW.y,
|
|
||||||
]);
|
|
||||||
let se = map.current.unproject([
|
|
||||||
x * cellSize + viewportNW.x + cellSize,
|
|
||||||
y * cellSize + viewportNW.y + cellSize,
|
|
||||||
]);
|
|
||||||
let sw = map.current.unproject([
|
|
||||||
x * cellSize + viewportNW.x,
|
|
||||||
y * cellSize + viewportNW.y + cellSize,
|
|
||||||
]);
|
|
||||||
const selectedCellLines: GeoJSON.FeatureCollection<
|
|
||||||
GeoJSON.Geometry,
|
|
||||||
GeoJSON.GeoJsonProperties
|
|
||||||
> = {
|
|
||||||
type: "FeatureCollection",
|
|
||||||
features: [
|
|
||||||
{
|
{
|
||||||
type: "Feature",
|
source: "grid-source",
|
||||||
geometry: {
|
id: features[0].id,
|
||||||
type: "LineString",
|
|
||||||
coordinates: [
|
|
||||||
[nw.lng, nw.lat],
|
|
||||||
[ne.lng, ne.lat],
|
|
||||||
[se.lng, se.lat],
|
|
||||||
[sw.lng, sw.lat],
|
|
||||||
[nw.lng, nw.lat],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
properties: { x: x, y: y },
|
|
||||||
},
|
},
|
||||||
],
|
{
|
||||||
};
|
selected: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
setSelectedCell(cell);
|
||||||
|
map.current.resize();
|
||||||
|
|
||||||
if (map.current.getLayer("selected-cell-layer")) {
|
var zoom = map.current.getZoom();
|
||||||
map.current.removeLayer("selected-cell-layer");
|
var viewportNW = map.current.project([-180, 85.051129]);
|
||||||
|
var cellSize = Math.pow(2, zoom + 2);
|
||||||
|
const x = features[0].properties!.x;
|
||||||
|
const y = features[0].properties!.y;
|
||||||
|
let nw = map.current.unproject([
|
||||||
|
x * cellSize + viewportNW.x,
|
||||||
|
y * cellSize + viewportNW.y,
|
||||||
|
]);
|
||||||
|
let ne = map.current.unproject([
|
||||||
|
x * cellSize + viewportNW.x + cellSize,
|
||||||
|
y * cellSize + viewportNW.y,
|
||||||
|
]);
|
||||||
|
let se = map.current.unproject([
|
||||||
|
x * cellSize + viewportNW.x + cellSize,
|
||||||
|
y * cellSize + viewportNW.y + cellSize,
|
||||||
|
]);
|
||||||
|
let sw = map.current.unproject([
|
||||||
|
x * cellSize + viewportNW.x,
|
||||||
|
y * cellSize + viewportNW.y + cellSize,
|
||||||
|
]);
|
||||||
|
const selectedCellLines: GeoJSON.FeatureCollection<
|
||||||
|
GeoJSON.Geometry,
|
||||||
|
GeoJSON.GeoJsonProperties
|
||||||
|
> = {
|
||||||
|
type: "FeatureCollection",
|
||||||
|
features: [
|
||||||
|
{
|
||||||
|
type: "Feature",
|
||||||
|
geometry: {
|
||||||
|
type: "LineString",
|
||||||
|
coordinates: [
|
||||||
|
[nw.lng, nw.lat],
|
||||||
|
[ne.lng, ne.lat],
|
||||||
|
[se.lng, se.lat],
|
||||||
|
[sw.lng, sw.lat],
|
||||||
|
[nw.lng, nw.lat],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
properties: { x: x, y: y },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
if (map.current.getLayer("selected-cell-layer")) {
|
||||||
|
map.current.removeLayer("selected-cell-layer");
|
||||||
|
}
|
||||||
|
if (map.current.getSource("selected-cell-source")) {
|
||||||
|
map.current.removeSource("selected-cell-source");
|
||||||
|
}
|
||||||
|
map.current.addSource("selected-cell-source", {
|
||||||
|
type: "geojson",
|
||||||
|
data: selectedCellLines,
|
||||||
|
});
|
||||||
|
map.current.addLayer({
|
||||||
|
id: "selected-cell-layer",
|
||||||
|
type: "line",
|
||||||
|
source: "selected-cell-source",
|
||||||
|
paint: {
|
||||||
|
"line-color": "blue",
|
||||||
|
"line-width": 3,
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (map.current.getSource("selected-cell-source")) {
|
}, 150);
|
||||||
map.current.removeSource("selected-cell-source");
|
});
|
||||||
}
|
|
||||||
map.current.addSource("selected-cell-source", {
|
map.current.on("dblclick", "grid-layer", (e) => {
|
||||||
type: "geojson",
|
if (singleClickTimeout) clearTimeout(singleClickTimeout);
|
||||||
data: selectedCellLines,
|
singleClickTimeout = null;
|
||||||
});
|
|
||||||
map.current.addLayer({
|
|
||||||
id: "selected-cell-layer",
|
|
||||||
type: "line",
|
|
||||||
source: "selected-cell-source",
|
|
||||||
paint: {
|
|
||||||
"line-color": "blue",
|
|
||||||
"line-width": 3,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}, [setSelectedCell, data]);
|
}, [setSelectedCell, data]);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user