Skip to content

Commit 7601da1

Browse files
committed
Add useEffect to zoom out of map when showGlobeButton is true
1 parent a626656 commit 7601da1

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/components/MlGlobeButton/MlGlobeButton.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface MlGlobeButtonProps {
1818
* Style object to adjust css definitions of the component.
1919
*/
2020
style?: CSSProperties;
21+
/**
22+
* Initial projection mode of the map.
23+
*/
24+
mode?: 'globe' | 'mercator';
2125
}
2226

2327
/**
@@ -37,19 +41,19 @@ const MlGlobeButton = (props: MlGlobeButtonProps) => {
3741
waitForLayer: props.insertBeforeLayer,
3842
});
3943

40-
const [projection, setProjection] = useState<'globe' | 'mercator'>('mercator');
44+
const [projection, setProjection] = useState<'globe' | 'mercator'>(props.mode || 'mercator');
4145

4246
useEffect(() => {
4347
const current = mapHook.map?.map.getProjection?.()?.type;
44-
if (current === 'globe' || current === 'mercator') {
45-
setProjection(current);
48+
if (current !== projection) {
49+
mapHook.map?.setProjection({ type: projection });
4650
}
4751
}, [mapHook.map]);
4852

4953
const handleClick = () => {
5054
if (!mapHook.map) return;
5155
const next = projection === 'globe' ? 'mercator' : 'globe';
52-
mapHook.map.map.setProjection({ type: next });
56+
mapHook.map.setProjection({ type: next });
5357
setProjection(next);
5458
};
5559

@@ -68,5 +72,6 @@ const MlGlobeButton = (props: MlGlobeButtonProps) => {
6872

6973
MlGlobeButton.defaultProps = {
7074
mapId: undefined,
75+
mode: 'mercator',
7176
};
7277
export default MlGlobeButton;

src/components/MlNavigationTools/MlNavigationTools.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ const MlNavigationTools = (props: MlNavigationToolsProps) => {
106106
mapHook.map.easeTo({ pitch: targetPitch });
107107
}, [mapHook.map]);
108108

109+
useEffect(() => {
110+
if (props.showGlobeButton && mapHook.map) {
111+
mapHook.map.easeTo({ zoom: 2 });
112+
}
113+
}, [props.showGlobeButton, mapHook.map]);
114+
109115
return (
110116
<Box
111117
sx={{
@@ -129,7 +135,7 @@ const MlNavigationTools = (props: MlNavigationToolsProps) => {
129135
{pitch < 29 ? '2D' : '3D'}
130136
</Button>
131137
)}
132-
{props.showGlobeButton && <MlGlobeButton />}
138+
{props.showGlobeButton && <MlGlobeButton mode="globe" />}
133139
{props.showFollowGpsButton && <MlFollowGps />}
134140
{props.showCenterLocationButton && <MlCenterPosition />}
135141
<ButtonGroup

0 commit comments

Comments
 (0)