Skip to content

Commit

Permalink
Merge pull request #743 from visualize-admin/fix/geo-offset
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne authored Sep 27, 2022
2 parents 504f9ba + a79e6ed commit 4c24a6e
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions app/charts/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ const useStyles = makeStyles<Theme>((theme) => ({

// Debounced function fixes the problem of maximizing window or opening a console,
// when map was not resized initially.
const resize = debounce((e: MapboxEvent, bbox: BBox) => {
if (e.originalEvent) {
e.target.resize();
e.target.fitBounds(bbox, { duration: 0 });
}
const resizeAndFit = debounce((map: mapboxgl.Map, bbox: BBox) => {
map.resize();
map.fitBounds(bbox, { duration: 0 });
}, 0);

export const MapComponent = () => {
Expand Down Expand Up @@ -306,6 +304,31 @@ export const MapComponent = () => {

const dataLoaded = features.areaLayer || features.symbolLayer;

const redrawMap = useEvent(() => {
if (!mapNodeRef.current) {
return;
}
const map = mapNodeRef.current.getMap() as mapboxgl.Map;
const bbox = map.getBounds().toArray() as BBox;
currentBBox.current = bbox;
resizeAndFit(map, lockedBBox || bbox);
});

const handleResize = useEvent((e: MapboxEvent) => {
if (e.originalEvent) {
redrawMap();
}
});

/**
* Redraw the map when style data has been downloaded to solve an offset problem between the viz
* layer and the base layer happening in Edge under precise conditions (Windows 10, Edge 105,
* resolution 1440x900).
*/
const handleStyleData = useEvent(() => {
redrawMap();
});

return (
<>
{locked ? null : (
Expand All @@ -322,6 +345,7 @@ export const MapComponent = () => {
initialViewState={defaultViewState}
mapLib={maplibregl}
mapStyle={mapStyle}
onStyleData={handleStyleData}
style={{
position: "absolute",
left: 0,
Expand All @@ -348,11 +372,7 @@ export const MapComponent = () => {

onViewStateChange(e);
}}
onResize={(e) => {
const bbox = e.target.getBounds().toArray() as BBox;
currentBBox.current = bbox;
resize(e, lockedBBox || bbox);
}}
onResize={handleResize}
{...viewState}
>
<DeckGLOverlay
Expand Down

1 comment on commit 4c24a6e

@vercel
Copy link

@vercel vercel bot commented on 4c24a6e Sep 27, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-alpha.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-git-main-ixt1.vercel.app

Please sign in to comment.