Skip to content

Commit

Permalink
Merge pull request #82 from aodn/features/5735-heatmap-layer
Browse files Browse the repository at this point in the history
Features/5735 heatmap layer
  • Loading branch information
utas-raymondng authored Jul 12, 2024
2 parents 487b63e + 22a1146 commit d936fd3
Show file tree
Hide file tree
Showing 12 changed files with 773 additions and 157 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/wellknown": "^0.5.8",
"axios": "^1.6.7",
"dayjs": "^1.11.10",
"events": "3.3.0",
"geojson": "0.5.0",
"http-proxy-middleware": "^2.0.6",
"lodash": "^4.17.21",
Expand Down
10 changes: 10 additions & 0 deletions src/components/common/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { merge } from "lodash";

const mergeWithDefaults = <T extends object>(
defaults: T,
props?: Partial<T>
): T => {
return merge({}, defaults, props);
};

export { mergeWithDefaults };
56 changes: 45 additions & 11 deletions src/components/map/mapbox/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState, useEffect, useContext } from "react";
import React, { useState, useEffect, useCallback, useRef } from "react";
import { Map, MapboxEvent, Style } from "mapbox-gl";
import MapContext from "./MapContext";
import "mapbox-gl/dist/mapbox-gl.css";
import ERSIWorldImagery from "./styles/ESRIWorldImagery.json";
import loadash from "lodash";

interface MapProps {
centerLongitude?: number;
Expand Down Expand Up @@ -54,6 +55,9 @@ const styles = [

const defaultStyle = 3;

// Magic number, try and error by experience
const DEBOUNCE_BEFORE_EVENT_FIRE = 1250;

const ReactMap = ({
panelId,
centerLongitude = 147.3353554138993,
Expand All @@ -66,6 +70,31 @@ const ReactMap = ({
}: React.PropsWithChildren<MapProps>) => {
const [map, setMap] = useState<Map | null>(null);

// Debouce to make the map transit smoother
const debounceOnZoomEvent = useRef(
loadash.debounce(
useCallback(
async (
event: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
) => onZoomEvent && onZoomEvent(event),
[onZoomEvent]
),
DEBOUNCE_BEFORE_EVENT_FIRE
)
).current;

const debounceOnMoveEvent = useRef(
loadash.debounce(
useCallback(
async (
event: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
) => onMoveEvent && onMoveEvent(event),
[onMoveEvent]
),
DEBOUNCE_BEFORE_EVENT_FIRE
)
).current;

useEffect(() => {
setMap((m) =>
m === null
Expand All @@ -82,20 +111,23 @@ const ReactMap = ({
);

if (map !== null) {
const zoomEvent = (
e: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
) => onZoomEvent && onZoomEvent(e);
// const zoomEvent = (
// e: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
// ) => onZoomEvent && onZoomEvent(e);

const moveEvent = (
e: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
) => onMoveEvent && onMoveEvent(e);
// const moveEvent = (
// e: MapboxEvent<MouseEvent | WheelEvent | TouchEvent | undefined>
// ) => onMoveEvent && onMoveEvent(e);

map.setProjection(projection);
// Stop drag cause map to rotate.
map.dragRotate.disable();

map.on("zoomend", zoomEvent);
map.on("moveend", moveEvent);
map.on("movestart", () => debounceOnZoomEvent.cancel());
map.on("zoomestart", () => debounceOnZoomEvent.cancel());

map.on("zoomend", debounceOnZoomEvent);
map.on("moveend", debounceOnMoveEvent);

// Create a resize observer to the canvas so we know if its size have changed
// and we need to redraw the map
Expand All @@ -107,8 +139,8 @@ const ReactMap = ({

return () => {
resizeObserver.unobserve(map.getContainer());
map.off("zoomend", zoomEvent);
map.off("moveend", moveEvent);
map.off("zoomend", debounceOnZoomEvent);
map.off("moveend", debounceOnMoveEvent);

// This cleanup all associated resources include controls, so no need to
// call removeControl()
Expand All @@ -125,6 +157,8 @@ const ReactMap = ({
map,
onZoomEvent,
onMoveEvent,
debounceOnZoomEvent,
debounceOnMoveEvent,
]);

return (
Expand Down
Loading

0 comments on commit d936fd3

Please sign in to comment.