Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/nowcasting-app/components/icons/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,16 @@ export const ZoomOutIcon = (props: React.SVGProps<SVGSVGElement> & { title: stri
</svg>
</span>
);

export const ResetIcon = () => {
return `<svg fill="#000000" viewBox="0 0 1920 1920" xmlns="http://www.w3.org/2000/svg">
<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
<g id="SVGRepo_iconCarrier">
<path d="M960 0v213.333c411.627 0 746.667 334.934 746.667 746.667S1371.627 1706.667 960 1706.667 213.333 1371.733 213.333 960c0-197.013 78.4-382.507 213.334-520.747v254.08H640V106.667H53.333V320h191.04C88.64 494.08 0 720.96 0 960c0 529.28 430.613 960 960 960s960-430.72 960-960S1489.387 0 960 0" fill-rule="evenodd">
</path>
</g>
</svg>`;
};
Comment on lines +392 to +400
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@braddf It is a string because we have to use it in html format, not sure if i should put it here or directly put it in
https://github.com/openclimatefix/quartz-frontend/pull/695/changes#diff-7251d9455f72e8fb43a93e7db2375b1429df6904295216039ede6b9da8fa5fe2R134


export default ZoomOutIcon;
41 changes: 41 additions & 0 deletions apps/nowcasting-app/components/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Dispatch, FC, SetStateAction, useEffect, useRef, useState } from "react
import { IMap } from "./types";
import useUpdateMapStateOnClick from "./use-update-map-state-on-click";
import useGlobalState from "../helpers/globalState";
import { ResetIcon } from "../icons/icons";
import {
AGGREGATION_LEVEL_MIN_ZOOM,
AGGREGATION_LEVELS,
Expand Down Expand Up @@ -61,6 +62,7 @@ const Map: FC<IMap> = ({
const [maps, setMaps] = useGlobalState("maps");
const [currentAggregation, setAggregation] = useGlobalState("aggregationLevel");
const [autoZoom] = useGlobalState("autoZoom");
const resetButtonDiv = useRef<HTMLDivElement | null>(null);

// Keep the latest autoZoom value available inside Mapbox event handlers (avoid stale closures)
const autozoomRef = useRef(autoZoom);
Expand All @@ -84,12 +86,26 @@ const Map: FC<IMap> = ({
console.log("setting map state");
const currentZoom = map.current?.getZoom() || 0;
const center = map.current?.getCenter();
const currentBearing = map.current?.getBearing() || 0;
const currentPitch = map.current?.getPitch() || 0;

setLng(Number(center?.lng.toFixed(4)));
setLat(Number(center?.lat.toFixed(4)));
setZoom(Number(currentZoom.toFixed(2)));

setAggregationLevelByCurrentZoom(currentZoom, autozoomRef.current, setAggregation);

// Check if map has been modified from default state
const mapModified =
currentZoom !== zoom || // Check if zoom has changed
center?.lng.toFixed(4) !== lng.toFixed(4) || // Check if longitude has changed
center?.lat.toFixed(4) !== lat.toFixed(4) || // Check if latitude has changed
currentBearing.toFixed(2) !== bearing.toFixed(2) || // Check if bearing has changed
currentPitch.toFixed(2) !== "0.00"; // Check if pitch has changed

if (mapModified) {
resetButtonDiv.current?.style.setProperty("display", "block");
}
};

if (map.current) return; // initialize map only once
Expand All @@ -108,6 +124,31 @@ const Map: FC<IMap> = ({

const nav = new mapboxgl.NavigationControl({ showCompass: false });
map.current.addControl(nav, "bottom-right");
map.current.addControl(
{
onAdd: function (m) {
const div = document.createElement("div");
div.className = "mapboxgl-ctrl mapboxgl-ctrl-group";
div.style.setProperty("display", "none");
div.innerHTML = `<button title="Reset Zoom" style="padding:7px;">${ResetIcon()}</button>`;
div.onclick = () => {
m.flyTo({
center: [lng, lat],
zoom: zoom,
pitch: 0,
bearing: 0,
duration: 1500,
essential: true
});
div.style.setProperty("display", "none");
};
resetButtonDiv.current = div;
return div;
},
onRemove: function () {}
},
"bottom-right"
);

map.current.on("load", (event) => {
setIsMapReady(true);
Expand Down
Loading