Skip to content

Commit 8e87a50

Browse files
committed
create GoogleMap and only handle camera change when map is active
1 parent e037895 commit 8e87a50

File tree

2 files changed

+73
-31
lines changed

2 files changed

+73
-31
lines changed

src/components/GoogleMap.tsx

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
Map as GoogleMapGL,
3+
APIProvider,
4+
MapCameraChangedEvent,
5+
} from "@vis.gl/react-google-maps";
6+
import { GoogleMapsSource, MapState } from "../types";
7+
import { useState } from "react";
8+
9+
interface GoogleMapProps {
10+
mapState: MapState;
11+
source: GoogleMapsSource;
12+
apiKey: string;
13+
synchronized: boolean;
14+
onMapChange: (state: Partial<MapState>) => void;
15+
onViewStateChange: (state: Partial<MapState>) => void;
16+
}
17+
18+
export function GoogleMap({
19+
mapState: effectiveMapState,
20+
source,
21+
apiKey,
22+
synchronized,
23+
onMapChange,
24+
onViewStateChange,
25+
}: GoogleMapProps) {
26+
const [isActive, setIsActive] = useState(false);
27+
28+
const handleCameraChange = (event: MapCameraChangedEvent) => {
29+
const newState: Partial<MapState> = {
30+
center: [event.detail.center.lng, event.detail.center.lat],
31+
zoom: event.detail.zoom,
32+
bearing: event.detail.heading ?? effectiveMapState.bearing,
33+
pitch: event.detail.tilt ?? effectiveMapState.pitch,
34+
};
35+
36+
if (synchronized) {
37+
onMapChange(newState);
38+
} else {
39+
onViewStateChange(newState);
40+
}
41+
};
42+
43+
return (
44+
<APIProvider apiKey={apiKey}>
45+
<div className="relative w-full h-full">
46+
<GoogleMapGL
47+
disableDefaultUI={true}
48+
center={{
49+
lat: effectiveMapState.center[1],
50+
lng: effectiveMapState.center[0],
51+
}}
52+
zoom={effectiveMapState.zoom}
53+
mapTypeId={source.mapType}
54+
heading={effectiveMapState.bearing}
55+
tilt={effectiveMapState.pitch}
56+
gestureHandling="greedy"
57+
onMouseover={() => setIsActive(true)}
58+
onMouseout={() => setIsActive(false)}
59+
onCameraChanged={isActive ? handleCameraChange : undefined}
60+
/>
61+
</div>
62+
</APIProvider>
63+
);
64+
}

src/components/Map.tsx

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Source,
66
NavigationControl,
77
} from "@vis.gl/react-maplibre";
8-
import { Map as GoogleMap, APIProvider } from "@vis.gl/react-google-maps";
8+
import { GoogleMap } from "./GoogleMap";
99
import { SourceSpecification } from "maplibre-gl";
1010

1111
import { MapState } from "../types";
@@ -95,36 +95,14 @@ export function Map({
9595
}
9696

9797
return (
98-
<APIProvider apiKey={state.apiKeys.googleMaps}>
99-
<div className="relative w-full h-full">
100-
<GoogleMap
101-
disableDefaultUI={true}
102-
center={{
103-
lat: effectiveMapState.center[1],
104-
lng: effectiveMapState.center[0],
105-
}}
106-
zoom={effectiveMapState.zoom}
107-
mapTypeId={source.mapType}
108-
heading={effectiveMapState.bearing}
109-
tilt={effectiveMapState.pitch}
110-
gestureHandling="greedy"
111-
onCameraChanged={({ detail: { center, zoom, heading, tilt } }) => {
112-
const newState: Partial<MapState> = {
113-
center: [center.lng, center.lat],
114-
zoom,
115-
bearing: heading ?? effectiveMapState.bearing,
116-
pitch: tilt ?? effectiveMapState.pitch,
117-
};
118-
119-
if (synchronized) {
120-
onMapChange(newState);
121-
} else {
122-
onViewStateChange(newState);
123-
}
124-
}}
125-
/>
126-
</div>
127-
</APIProvider>
98+
<GoogleMap
99+
mapState={effectiveMapState}
100+
source={source}
101+
apiKey={state.apiKeys.googleMaps}
102+
synchronized={synchronized}
103+
onMapChange={onMapChange}
104+
onViewStateChange={onViewStateChange}
105+
/>
128106
);
129107
}
130108

0 commit comments

Comments
 (0)