google-maps-react-markers 2.0.15 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE.md +1 -1
- package/README.md +60 -198
- package/dist/index.d.ts +206 -285
- package/dist/index.js +989 -2
- package/dist/index.js.map +1 -0
- package/package.json +80 -112
- package/dist/index.cjs +0 -2
- package/dist/index.d.cts +0 -295
package/dist/index.js
CHANGED
|
@@ -1,2 +1,989 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
import { createContext, use, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { importLibrary, setOptions } from "@googlemaps/js-api-loader";
|
|
4
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
// src/context.ts
|
|
7
|
+
const GoogleMapContext = createContext(null);
|
|
8
|
+
const useGoogleMapContext = (component) => {
|
|
9
|
+
const context = use(GoogleMapContext);
|
|
10
|
+
if (!context) throw new Error(`[google-maps-react-markers] <${component}> must be rendered inside <GoogleMap>.`);
|
|
11
|
+
return context;
|
|
12
|
+
};
|
|
13
|
+
// src/loader.ts
|
|
14
|
+
const isDev$2 = process.env.NODE_ENV !== "production";
|
|
15
|
+
let configured = false;
|
|
16
|
+
/** The page loaded the API on its own (script tag or another loader). */
|
|
17
|
+
let external = false;
|
|
18
|
+
/** Resolves once a Maps script added by the page has finished loading. */
|
|
19
|
+
let externalReady = null;
|
|
20
|
+
let configuredKey;
|
|
21
|
+
let status = "loading";
|
|
22
|
+
let mapsPromise = null;
|
|
23
|
+
const libraries = /* @__PURE__ */ new Map();
|
|
24
|
+
const libraryPromises = /* @__PURE__ */ new Map();
|
|
25
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
26
|
+
const emit = () => {
|
|
27
|
+
for (const listener of listeners) listener();
|
|
28
|
+
};
|
|
29
|
+
const setStatus = (next, nextError = null) => {
|
|
30
|
+
status = next;
|
|
31
|
+
emit();
|
|
32
|
+
};
|
|
33
|
+
/** Subscribes to loader changes (status and loaded libraries). */
|
|
34
|
+
const subscribe = (listener) => {
|
|
35
|
+
listeners.add(listener);
|
|
36
|
+
return () => {
|
|
37
|
+
listeners.delete(listener);
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const getStatus = () => status;
|
|
41
|
+
const getServerStatus = () => "loading";
|
|
42
|
+
const isConfigured = () => configured;
|
|
43
|
+
const getLoadedLibrary = (name) => libraries.get(name) ?? null;
|
|
44
|
+
const configure = (options) => {
|
|
45
|
+
if (configured) {
|
|
46
|
+
if (isDev$2 && options.apiKey !== configuredKey) console.warn("[google-maps-react-markers] The Google Maps API is already loaded with other options. Only the options of the first <GoogleMap> are used.");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
configured = true;
|
|
50
|
+
configuredKey = options.apiKey;
|
|
51
|
+
const previousAuthFailure = window.gm_authFailure;
|
|
52
|
+
window.gm_authFailure = () => {
|
|
53
|
+
previousAuthFailure?.();
|
|
54
|
+
setStatus("error", /* @__PURE__ */ new Error("Google Maps rejected the API key. Check the key and its HTTP referrer restrictions."));
|
|
55
|
+
};
|
|
56
|
+
if (window.google?.maps?.importLibrary) {
|
|
57
|
+
external = true;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const script = document.querySelector("script[src*=\"maps.googleapis.com/maps/api/js\"]");
|
|
61
|
+
if (script) {
|
|
62
|
+
external = true;
|
|
63
|
+
externalReady = new Promise((resolve, reject) => {
|
|
64
|
+
script.addEventListener("load", () => resolve(), { once: true });
|
|
65
|
+
script.addEventListener("error", () => reject(/* @__PURE__ */ new Error("The Google Maps script on the page failed to load.")), { once: true });
|
|
66
|
+
});
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const apiOptions = {
|
|
70
|
+
key: options.apiKey,
|
|
71
|
+
v: options.version ?? "weekly",
|
|
72
|
+
language: options.language,
|
|
73
|
+
region: options.region,
|
|
74
|
+
mapIds: options.mapIds?.length ? options.mapIds : void 0,
|
|
75
|
+
authReferrerPolicy: options.authReferrerPolicy,
|
|
76
|
+
channel: options.channel,
|
|
77
|
+
solutionChannel: "GMP_GMRM_v3"
|
|
78
|
+
};
|
|
79
|
+
const defined = Object.fromEntries(Object.entries(apiOptions).filter(([, value]) => value !== void 0 && value !== ""));
|
|
80
|
+
setOptions(defined);
|
|
81
|
+
emit();
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Loads a Maps JavaScript API library once and caches it.
|
|
85
|
+
* Waits for the first `<GoogleMap>` to configure the loader.
|
|
86
|
+
*/
|
|
87
|
+
const loadLibrary = (name) => {
|
|
88
|
+
let promise = libraryPromises.get(name);
|
|
89
|
+
if (!promise) {
|
|
90
|
+
promise = (external ? (externalReady ?? Promise.resolve()).then(() => google.maps.importLibrary(name)) : importLibrary(name)).then((library) => {
|
|
91
|
+
libraries.set(name, library);
|
|
92
|
+
emit();
|
|
93
|
+
return library;
|
|
94
|
+
});
|
|
95
|
+
libraryPromises.set(name, promise);
|
|
96
|
+
}
|
|
97
|
+
return promise;
|
|
98
|
+
};
|
|
99
|
+
/** Configures the loader with the first options it receives and loads the core and maps libraries. */
|
|
100
|
+
const loadGoogleMaps = (options) => {
|
|
101
|
+
configure(options);
|
|
102
|
+
if (!mapsPromise) {
|
|
103
|
+
const names = [
|
|
104
|
+
"core",
|
|
105
|
+
"maps",
|
|
106
|
+
...options.libraries ?? []
|
|
107
|
+
];
|
|
108
|
+
mapsPromise = Promise.all(names.map((name) => loadLibrary(name))).then(() => {
|
|
109
|
+
if (status !== "error") setStatus("ready");
|
|
110
|
+
}, (reason) => {
|
|
111
|
+
setStatus("error", reason instanceof Error ? reason : new Error(String(reason)));
|
|
112
|
+
throw reason;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return mapsPromise;
|
|
116
|
+
};
|
|
117
|
+
// src/hooks.ts
|
|
118
|
+
/**
|
|
119
|
+
* The `google.maps.Map` instance of the closest `<GoogleMap>`, or `null` while it is loading
|
|
120
|
+
* (and on the server). Throws when used outside `<GoogleMap>`: use its `onLoad` prop there.
|
|
121
|
+
*/
|
|
122
|
+
const useMap = () => {
|
|
123
|
+
const context = use(GoogleMapContext);
|
|
124
|
+
if (!context) throw new Error("[google-maps-react-markers] useMap() must be used inside <GoogleMap>. Outside it, use onLoad.");
|
|
125
|
+
return context.map;
|
|
126
|
+
};
|
|
127
|
+
/** Loading status of the Google Maps JavaScript API. Always `'loading'` on the server. */
|
|
128
|
+
const useGoogleMapsStatus = () => useSyncExternalStore(subscribe, getStatus, getServerStatus);
|
|
129
|
+
const getServerLibrary = () => null;
|
|
130
|
+
/**
|
|
131
|
+
* Loads a Maps JavaScript API library, for example `useMapsLibrary('places')`.
|
|
132
|
+
* Returns `null` until it is loaded (and on the server). Works anywhere in the page,
|
|
133
|
+
* as soon as a `<GoogleMap>` has configured the loader.
|
|
134
|
+
*/
|
|
135
|
+
const useMapsLibrary = (name) => {
|
|
136
|
+
const library = useSyncExternalStore(subscribe, () => getLoadedLibrary(name), getServerLibrary);
|
|
137
|
+
const configured = useSyncExternalStore(subscribe, isConfigured, () => false);
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (configured && !library) loadLibrary(name).catch(() => {});
|
|
140
|
+
}, [
|
|
141
|
+
configured,
|
|
142
|
+
library,
|
|
143
|
+
name
|
|
144
|
+
]);
|
|
145
|
+
return library;
|
|
146
|
+
};
|
|
147
|
+
// src/internal/geometry.ts
|
|
148
|
+
/** Compares two numeric arrays element by element, within `eps`. Empty arrays are never equal. */
|
|
149
|
+
const isArraysEqualEps = (arrayA, arrayB, eps = 1e-6) => {
|
|
150
|
+
if (!arrayA.length || arrayA.length !== arrayB.length) return false;
|
|
151
|
+
for (let i = 0; i < arrayA.length; i++) if (Math.abs(arrayA[i] - arrayB[i]) > eps) return false;
|
|
152
|
+
return true;
|
|
153
|
+
};
|
|
154
|
+
/** Converts map bounds to `[westLng, southLat, eastLng, northLat]`, the format used by supercluster. */
|
|
155
|
+
const boundsToBbox = (bounds) => {
|
|
156
|
+
const ne = bounds.getNorthEast();
|
|
157
|
+
const sw = bounds.getSouthWest();
|
|
158
|
+
return [
|
|
159
|
+
sw.lng(),
|
|
160
|
+
sw.lat(),
|
|
161
|
+
ne.lng(),
|
|
162
|
+
ne.lat()
|
|
163
|
+
];
|
|
164
|
+
};
|
|
165
|
+
const toLatLngLiteral = (value) => {
|
|
166
|
+
if (!value) return null;
|
|
167
|
+
if (typeof value.lat === "function") {
|
|
168
|
+
const latLng = value;
|
|
169
|
+
return {
|
|
170
|
+
lat: latLng.lat(),
|
|
171
|
+
lng: latLng.lng()
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const literal = value;
|
|
175
|
+
return {
|
|
176
|
+
lat: literal.lat,
|
|
177
|
+
lng: literal.lng
|
|
178
|
+
};
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Equality for map options: shallow, plus a structural check for nested values
|
|
182
|
+
* (`styles`, `restriction`), so inline objects do not call setOptions on every render.
|
|
183
|
+
*/
|
|
184
|
+
const optionsEqual = (a, b) => {
|
|
185
|
+
if (shallowEqual(a, b)) return true;
|
|
186
|
+
if (!a || !b) return false;
|
|
187
|
+
const keysA = Object.keys(a);
|
|
188
|
+
if (keysA.length !== Object.keys(b).length) return false;
|
|
189
|
+
return keysA.every((key) => {
|
|
190
|
+
const valueA = a[key];
|
|
191
|
+
const valueB = b[key];
|
|
192
|
+
if (Object.is(valueA, valueB)) return true;
|
|
193
|
+
if (typeof valueA !== "object" || typeof valueB !== "object" || !valueA || !valueB) return false;
|
|
194
|
+
try {
|
|
195
|
+
return JSON.stringify(valueA) === JSON.stringify(valueB);
|
|
196
|
+
} catch {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
};
|
|
201
|
+
/** Shallow equality for plain option objects. */
|
|
202
|
+
const shallowEqual = (a, b) => {
|
|
203
|
+
if (a === b) return true;
|
|
204
|
+
if (!a || !b) return false;
|
|
205
|
+
const keysA = Object.keys(a);
|
|
206
|
+
if (keysA.length !== Object.keys(b).length) return false;
|
|
207
|
+
return keysA.every((key) => Object.is(a[key], b[key]));
|
|
208
|
+
};
|
|
209
|
+
// src/internal/marker-handle.ts
|
|
210
|
+
/** Set on every marker container: map mouse events coming from a marker are ignored. */
|
|
211
|
+
const MARKER_ATTRIBUTE = "data-gmrm-marker";
|
|
212
|
+
// src/internal/map-events.ts
|
|
213
|
+
/** Prop name → Google Maps event name. Mouse events receive `(event, map)`, the others `(map)`. */
|
|
214
|
+
const MAP_EVENTS = {
|
|
215
|
+
onBoundsChanged: "bounds_changed",
|
|
216
|
+
onCenterChanged: "center_changed",
|
|
217
|
+
onClick: "click",
|
|
218
|
+
onContextMenu: "contextmenu",
|
|
219
|
+
onDoubleClick: "dblclick",
|
|
220
|
+
onDrag: "drag",
|
|
221
|
+
onDragEnd: "dragend",
|
|
222
|
+
onDragStart: "dragstart",
|
|
223
|
+
onHeadingChanged: "heading_changed",
|
|
224
|
+
onIdle: "idle",
|
|
225
|
+
onMapTypeIdChanged: "maptypeid_changed",
|
|
226
|
+
onMouseMove: "mousemove",
|
|
227
|
+
onMouseOut: "mouseout",
|
|
228
|
+
onMouseOver: "mouseover",
|
|
229
|
+
onTilesLoaded: "tilesloaded",
|
|
230
|
+
onTiltChanged: "tilt_changed",
|
|
231
|
+
onZoomChanged: "zoom_changed"
|
|
232
|
+
};
|
|
233
|
+
const MOUSE_EVENTS = /* @__PURE__ */ new Set([
|
|
234
|
+
"onClick",
|
|
235
|
+
"onContextMenu",
|
|
236
|
+
"onDoubleClick",
|
|
237
|
+
"onMouseMove",
|
|
238
|
+
"onMouseOut",
|
|
239
|
+
"onMouseOver"
|
|
240
|
+
]);
|
|
241
|
+
const MAP_EVENT_PROPS = Object.keys(MAP_EVENTS);
|
|
242
|
+
// src/internal/use-latest.ts
|
|
243
|
+
/** A ref that always holds the latest value. Read it in event handlers, never during render. */
|
|
244
|
+
const useLatest = (value) => {
|
|
245
|
+
const ref = useRef(value);
|
|
246
|
+
useLayoutEffect(() => {
|
|
247
|
+
ref.current = value;
|
|
248
|
+
});
|
|
249
|
+
return ref;
|
|
250
|
+
};
|
|
251
|
+
// src/google-map.tsx
|
|
252
|
+
const isDev$1 = process.env.NODE_ENV !== "production";
|
|
253
|
+
/**
|
|
254
|
+
* Whether a map mouse event happened over a marker. Google reports the event on its own
|
|
255
|
+
* layer, so the target is not enough: check what is under the pointer too.
|
|
256
|
+
*/
|
|
257
|
+
const isOnMarker = (event) => {
|
|
258
|
+
const domEvent = event?.domEvent;
|
|
259
|
+
if (!domEvent) return false;
|
|
260
|
+
const selector = `[${MARKER_ATTRIBUTE}]`;
|
|
261
|
+
if (domEvent.target instanceof Element && domEvent.target.closest(selector)) return true;
|
|
262
|
+
if (!("clientX" in domEvent) || typeof document.elementsFromPoint !== "function") return false;
|
|
263
|
+
return document.elementsFromPoint(domEvent.clientX, domEvent.clientY).some((element) => element.closest(selector) !== null);
|
|
264
|
+
};
|
|
265
|
+
const baseStyle = {
|
|
266
|
+
position: "relative",
|
|
267
|
+
overflow: "hidden"
|
|
268
|
+
};
|
|
269
|
+
/** Without a className the map fills its parent. With one, the class decides the size. */
|
|
270
|
+
const fillStyle = {
|
|
271
|
+
...baseStyle,
|
|
272
|
+
width: "100%",
|
|
273
|
+
height: "100%"
|
|
274
|
+
};
|
|
275
|
+
const layerStyle = {
|
|
276
|
+
position: "absolute",
|
|
277
|
+
inset: 0
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* A Google Map. Renders `loadingContent` on the server and while the API loads, then the map.
|
|
281
|
+
* Put `<Marker>`, `<MapControl>` and components that use `useMap()` inside it.
|
|
282
|
+
*/
|
|
283
|
+
const GoogleMap = (props) => {
|
|
284
|
+
const { apiKey, version, language, region, libraries, mapIds, authReferrerPolicy, channel, mapId, markerRenderer = "auto", center, zoom, options, loadingContent = null, errorContent = null, className, style, id, ref, children } = props;
|
|
285
|
+
const mapDivRef = useRef(null);
|
|
286
|
+
const latest = useLatest(props);
|
|
287
|
+
const [map, setMap] = useState(null);
|
|
288
|
+
const [failed, setFailed] = useState(false);
|
|
289
|
+
const status = useGoogleMapsStatus();
|
|
290
|
+
useEffect(() => {
|
|
291
|
+
const div = mapDivRef.current;
|
|
292
|
+
if (!div) return void 0;
|
|
293
|
+
let cancelled = false;
|
|
294
|
+
let instance = null;
|
|
295
|
+
const initial = latest.current;
|
|
296
|
+
loadGoogleMaps({
|
|
297
|
+
apiKey,
|
|
298
|
+
version,
|
|
299
|
+
language,
|
|
300
|
+
region,
|
|
301
|
+
libraries,
|
|
302
|
+
mapIds,
|
|
303
|
+
authReferrerPolicy,
|
|
304
|
+
channel
|
|
305
|
+
}).then(() => mapId ? loadLibrary("marker") : void 0).then(() => {
|
|
306
|
+
if (cancelled) return;
|
|
307
|
+
if (isDev$1 && !(initial.center ?? initial.defaultCenter) && initial.zoom === void 0 && initial.defaultZoom === void 0) console.warn("[google-maps-react-markers] Pass center/defaultCenter and zoom/defaultZoom, or the map stays blank.");
|
|
308
|
+
instance = new google.maps.Map(div, {
|
|
309
|
+
...initial.options,
|
|
310
|
+
center: initial.center ?? initial.defaultCenter,
|
|
311
|
+
zoom: initial.zoom ?? initial.defaultZoom,
|
|
312
|
+
mapId
|
|
313
|
+
});
|
|
314
|
+
setMap(instance);
|
|
315
|
+
latest.current.onLoad?.({
|
|
316
|
+
map: instance,
|
|
317
|
+
maps: google.maps
|
|
318
|
+
});
|
|
319
|
+
}).catch((error) => {
|
|
320
|
+
if (cancelled) return;
|
|
321
|
+
if (isDev$1) console.error("[google-maps-react-markers] The map could not be created.", error);
|
|
322
|
+
setFailed(true);
|
|
323
|
+
});
|
|
324
|
+
return () => {
|
|
325
|
+
cancelled = true;
|
|
326
|
+
if (instance) {
|
|
327
|
+
google.maps.event.clearInstanceListeners(instance);
|
|
328
|
+
setMap(null);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}, [mapId]);
|
|
332
|
+
const appliedOptions = useRef(options);
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
if (!map || optionsEqual(appliedOptions.current, options)) return;
|
|
335
|
+
appliedOptions.current = options;
|
|
336
|
+
if (options) map.setOptions(options);
|
|
337
|
+
}, [map, options]);
|
|
338
|
+
const centerLat = center?.lat;
|
|
339
|
+
const centerLng = center?.lng;
|
|
340
|
+
useEffect(() => {
|
|
341
|
+
if (!map || centerLat === void 0 || centerLng === void 0) return;
|
|
342
|
+
const current = map.getCenter();
|
|
343
|
+
if (current && isArraysEqualEps([current.lat(), current.lng()], [centerLat, centerLng], 1e-9)) return;
|
|
344
|
+
map.setCenter({
|
|
345
|
+
lat: centerLat,
|
|
346
|
+
lng: centerLng
|
|
347
|
+
});
|
|
348
|
+
}, [
|
|
349
|
+
map,
|
|
350
|
+
centerLat,
|
|
351
|
+
centerLng
|
|
352
|
+
]);
|
|
353
|
+
useEffect(() => {
|
|
354
|
+
if (!map || zoom === void 0 || map.getZoom() === zoom) return;
|
|
355
|
+
map.setZoom(zoom);
|
|
356
|
+
}, [map, zoom]);
|
|
357
|
+
const activeEvents = MAP_EVENT_PROPS.filter((name) => props[name]).join(",");
|
|
358
|
+
useEffect(() => {
|
|
359
|
+
if (!map || !activeEvents) return void 0;
|
|
360
|
+
const listeners = activeEvents.split(",").map((name) => {
|
|
361
|
+
const prop = name;
|
|
362
|
+
return map.addListener(MAP_EVENTS[prop], (event) => {
|
|
363
|
+
const handler = latest.current[prop];
|
|
364
|
+
if (!handler) return;
|
|
365
|
+
if (MOUSE_EVENTS.has(prop) && isOnMarker(event)) return;
|
|
366
|
+
if (MOUSE_EVENTS.has(prop) && event) handler(event, map);
|
|
367
|
+
else handler(map);
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
return () => {
|
|
371
|
+
for (const listener of listeners) listener.remove();
|
|
372
|
+
};
|
|
373
|
+
}, [
|
|
374
|
+
map,
|
|
375
|
+
activeEvents,
|
|
376
|
+
latest
|
|
377
|
+
]);
|
|
378
|
+
useEffect(() => {
|
|
379
|
+
if (!map) return void 0;
|
|
380
|
+
let previous = [];
|
|
381
|
+
const listener = map.addListener("idle", () => {
|
|
382
|
+
const onChange = latest.current.onChange;
|
|
383
|
+
const bounds = map.getBounds();
|
|
384
|
+
const mapCenter = map.getCenter();
|
|
385
|
+
const mapZoom = map.getZoom();
|
|
386
|
+
if (!onChange || !bounds || !mapCenter || mapZoom === void 0) return;
|
|
387
|
+
const bbox = boundsToBbox(bounds);
|
|
388
|
+
const next = [...bbox, mapZoom];
|
|
389
|
+
if (isArraysEqualEps(next, previous)) return;
|
|
390
|
+
previous = next;
|
|
391
|
+
onChange({
|
|
392
|
+
map,
|
|
393
|
+
bounds,
|
|
394
|
+
bbox,
|
|
395
|
+
zoom: mapZoom,
|
|
396
|
+
center: {
|
|
397
|
+
lat: mapCenter.lat(),
|
|
398
|
+
lng: mapCenter.lng()
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
return () => listener.remove();
|
|
403
|
+
}, [map, latest]);
|
|
404
|
+
const renderer = !map ? null : markerRenderer === "overlay" || !mapId ? "overlay" : "advanced";
|
|
405
|
+
useEffect(() => {
|
|
406
|
+
if (isDev$1 && markerRenderer === "advanced" && !mapId) console.warn("[google-maps-react-markers] markerRenderer=\"advanced\" needs a mapId. Falling back to \"overlay\".");
|
|
407
|
+
}, [markerRenderer, mapId]);
|
|
408
|
+
const contextValue = useMemo(() => ({
|
|
409
|
+
map,
|
|
410
|
+
renderer,
|
|
411
|
+
status
|
|
412
|
+
}), [
|
|
413
|
+
map,
|
|
414
|
+
renderer,
|
|
415
|
+
status
|
|
416
|
+
]);
|
|
417
|
+
const hasError = status === "error" || failed;
|
|
418
|
+
const loading = !hasError && (status !== "ready" || !map);
|
|
419
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
420
|
+
ref,
|
|
421
|
+
id,
|
|
422
|
+
className,
|
|
423
|
+
style: {
|
|
424
|
+
...className ? baseStyle : fillStyle,
|
|
425
|
+
...style
|
|
426
|
+
},
|
|
427
|
+
"aria-busy": loading || void 0,
|
|
428
|
+
children: [
|
|
429
|
+
/* @__PURE__ */ jsx("div", {
|
|
430
|
+
ref: mapDivRef,
|
|
431
|
+
style: layerStyle,
|
|
432
|
+
"data-gmrm-map": ""
|
|
433
|
+
}, mapId ?? ""),
|
|
434
|
+
hasError ? /* @__PURE__ */ jsx("div", {
|
|
435
|
+
role: "alert",
|
|
436
|
+
style: layerStyle,
|
|
437
|
+
children: errorContent
|
|
438
|
+
}) : loading ? /* @__PURE__ */ jsx("div", {
|
|
439
|
+
style: layerStyle,
|
|
440
|
+
children: loadingContent
|
|
441
|
+
}) : null,
|
|
442
|
+
/* @__PURE__ */ jsx(GoogleMapContext, {
|
|
443
|
+
value: contextValue,
|
|
444
|
+
children
|
|
445
|
+
})
|
|
446
|
+
]
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
// src/map-control.tsx
|
|
450
|
+
/** Renders React content as a native map control, for example `<MapControl position="TOP_RIGHT">`. */
|
|
451
|
+
const MapControl = ({ position, children }) => {
|
|
452
|
+
const { map } = useGoogleMapContext("MapControl");
|
|
453
|
+
const [container, setContainer] = useState(null);
|
|
454
|
+
useEffect(() => {
|
|
455
|
+
if (!map) return void 0;
|
|
456
|
+
const element = document.createElement("div");
|
|
457
|
+
const controlPosition = typeof position === "string" ? google.maps.ControlPosition[position] : position;
|
|
458
|
+
const controls = map.controls[controlPosition];
|
|
459
|
+
if (!controls) return void 0;
|
|
460
|
+
controls.push(element);
|
|
461
|
+
setContainer(element);
|
|
462
|
+
return () => {
|
|
463
|
+
const index = controls.getArray().indexOf(element);
|
|
464
|
+
if (index !== -1) controls.removeAt(index);
|
|
465
|
+
setContainer(null);
|
|
466
|
+
};
|
|
467
|
+
}, [map, position]);
|
|
468
|
+
return container ? createPortal(children, container) : null;
|
|
469
|
+
};
|
|
470
|
+
// src/internal/advanced-marker.ts
|
|
471
|
+
/** A marker backed by `google.maps.marker.AdvancedMarkerElement`. Needs a map with a `mapId`. */
|
|
472
|
+
const createAdvancedMarker = ({ map, container, getHandlers }) => {
|
|
473
|
+
const marker = new google.maps.marker.AdvancedMarkerElement();
|
|
474
|
+
const supportsAnchor = "anchorLeft" in marker;
|
|
475
|
+
marker.append(container);
|
|
476
|
+
const dragData = (event) => ({ latLng: toLatLngLiteral(event.latLng) ?? toLatLngLiteral(marker.position) ?? {
|
|
477
|
+
lat: 0,
|
|
478
|
+
lng: 0
|
|
479
|
+
} });
|
|
480
|
+
const onClick = (event) => getHandlers().onClick?.(event);
|
|
481
|
+
const dragListeners = [
|
|
482
|
+
["dragstart", "onDragStart"],
|
|
483
|
+
["drag", "onDrag"],
|
|
484
|
+
["dragend", "onDragEnd"]
|
|
485
|
+
].map(([name, handler]) => marker.addListener(name, (event) => getHandlers()[handler]?.(event.domEvent, dragData(event))));
|
|
486
|
+
marker.addEventListener("gmp-click", onClick);
|
|
487
|
+
marker.map = map;
|
|
488
|
+
return {
|
|
489
|
+
setPosition: (position) => {
|
|
490
|
+
marker.position = position;
|
|
491
|
+
},
|
|
492
|
+
setAnchor: ({ x, y }) => {
|
|
493
|
+
if (supportsAnchor) {
|
|
494
|
+
marker.anchorLeft = `${-x * 100}%`;
|
|
495
|
+
marker.anchorTop = `${-y * 100}%`;
|
|
496
|
+
} else container.style.transform = `translate(${(.5 - x) * 100}%, ${(1 - y) * 100}%)`;
|
|
497
|
+
},
|
|
498
|
+
setClassName: (className) => {
|
|
499
|
+
container.className = className ?? "";
|
|
500
|
+
},
|
|
501
|
+
setZIndex: (zIndex) => {
|
|
502
|
+
marker.zIndex = zIndex ?? null;
|
|
503
|
+
},
|
|
504
|
+
setDraggable: (draggable) => {
|
|
505
|
+
marker.gmpDraggable = draggable;
|
|
506
|
+
},
|
|
507
|
+
setTitle: (title) => {
|
|
508
|
+
marker.title = title ?? "";
|
|
509
|
+
},
|
|
510
|
+
setClickable: (clickable) => {
|
|
511
|
+
marker.gmpClickable = clickable;
|
|
512
|
+
},
|
|
513
|
+
setCollisionBehavior: (behavior) => {
|
|
514
|
+
marker.collisionBehavior = behavior ?? null;
|
|
515
|
+
},
|
|
516
|
+
destroy: () => {
|
|
517
|
+
marker.removeEventListener("gmp-click", onClick);
|
|
518
|
+
for (const listener of dragListeners) listener.remove();
|
|
519
|
+
marker.map = null;
|
|
520
|
+
marker.remove();
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
};
|
|
524
|
+
// src/internal/anchor.ts
|
|
525
|
+
const PRESETS = {
|
|
526
|
+
center: {
|
|
527
|
+
x: .5,
|
|
528
|
+
y: .5
|
|
529
|
+
},
|
|
530
|
+
top: {
|
|
531
|
+
x: .5,
|
|
532
|
+
y: 0
|
|
533
|
+
},
|
|
534
|
+
bottom: {
|
|
535
|
+
x: .5,
|
|
536
|
+
y: 1
|
|
537
|
+
},
|
|
538
|
+
left: {
|
|
539
|
+
x: 0,
|
|
540
|
+
y: .5
|
|
541
|
+
},
|
|
542
|
+
right: {
|
|
543
|
+
x: 1,
|
|
544
|
+
y: .5
|
|
545
|
+
},
|
|
546
|
+
"top-left": {
|
|
547
|
+
x: 0,
|
|
548
|
+
y: 0
|
|
549
|
+
},
|
|
550
|
+
"top-right": {
|
|
551
|
+
x: 1,
|
|
552
|
+
y: 0
|
|
553
|
+
},
|
|
554
|
+
"bottom-left": {
|
|
555
|
+
x: 0,
|
|
556
|
+
y: 1
|
|
557
|
+
},
|
|
558
|
+
"bottom-right": {
|
|
559
|
+
x: 1,
|
|
560
|
+
y: 1
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
const resolveAnchor = (anchor = "center") => typeof anchor === "string" ? PRESETS[anchor] : anchor;
|
|
564
|
+
/** Stable key for an anchor, so effects re-run only when it really changes. */
|
|
565
|
+
const anchorKey = (anchor) => {
|
|
566
|
+
const { x, y } = resolveAnchor(anchor);
|
|
567
|
+
return `${x},${y}`;
|
|
568
|
+
};
|
|
569
|
+
// src/internal/a11y.ts
|
|
570
|
+
/** Same wording and keys as Google's advanced markers, so both renderers behave alike. */
|
|
571
|
+
const DRAG_HELP = "To activate drag with keyboard, press Alt + Enter. Once in keyboard drag state, use the arrow keys to move the marker. To complete the drag, press the Enter key. To cancel, press Escape.";
|
|
572
|
+
const HELP_ID = "gmrm-drag-help";
|
|
573
|
+
const LIVE_ID = "gmrm-live";
|
|
574
|
+
const STYLE_ID = "gmrm-styles";
|
|
575
|
+
const visuallyHidden = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);clip-path:inset(50%);white-space:nowrap;border:0";
|
|
576
|
+
/**
|
|
577
|
+
* Focus ring for overlay markers: a white ring inside a blue one, visible on light and dark maps
|
|
578
|
+
* (WCAG 2.4.13 Focus Appearance). `:where()` keeps specificity at zero, so any class overrides it.
|
|
579
|
+
*/
|
|
580
|
+
const css = `:where([${MARKER_ATTRIBUTE}][tabindex]):focus-visible{outline:3px solid #0b57d0;outline-offset:2px;box-shadow:0 0 0 2px #fff;border-radius:4px}`;
|
|
581
|
+
const ensureElement = (id, create) => {
|
|
582
|
+
const existing = document.getElementById(id);
|
|
583
|
+
if (existing) return existing;
|
|
584
|
+
const element = create();
|
|
585
|
+
element.id = id;
|
|
586
|
+
document.body.append(element);
|
|
587
|
+
return element;
|
|
588
|
+
};
|
|
589
|
+
/** Adds the focus ring style once per document. */
|
|
590
|
+
const ensureMarkerStyles = () => {
|
|
591
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
592
|
+
const style = document.createElement("style");
|
|
593
|
+
style.id = STYLE_ID;
|
|
594
|
+
style.textContent = css;
|
|
595
|
+
document.head.append(style);
|
|
596
|
+
};
|
|
597
|
+
/** The id of a hidden element with the keyboard drag instructions, for aria-describedby. */
|
|
598
|
+
const dragHelpId = () => {
|
|
599
|
+
ensureElement(HELP_ID, () => {
|
|
600
|
+
const element = document.createElement("div");
|
|
601
|
+
element.hidden = true;
|
|
602
|
+
element.textContent = DRAG_HELP;
|
|
603
|
+
return element;
|
|
604
|
+
});
|
|
605
|
+
return HELP_ID;
|
|
606
|
+
};
|
|
607
|
+
let announceTimer;
|
|
608
|
+
/** Speaks a short message through a shared polite live region. */
|
|
609
|
+
const announce = (message) => {
|
|
610
|
+
const region = ensureElement(LIVE_ID, () => {
|
|
611
|
+
const element = document.createElement("div");
|
|
612
|
+
element.setAttribute("role", "status");
|
|
613
|
+
element.setAttribute("aria-live", "polite");
|
|
614
|
+
element.setAttribute("aria-atomic", "true");
|
|
615
|
+
element.style.cssText = visuallyHidden;
|
|
616
|
+
return element;
|
|
617
|
+
});
|
|
618
|
+
region.textContent = "";
|
|
619
|
+
clearTimeout(announceTimer);
|
|
620
|
+
announceTimer = setTimeout(() => {
|
|
621
|
+
region.textContent = message;
|
|
622
|
+
}, 50);
|
|
623
|
+
};
|
|
624
|
+
// src/internal/overlay-marker.ts
|
|
625
|
+
/** Pixels per arrow key press during a keyboard drag; Shift moves five times further. */
|
|
626
|
+
const KEYBOARD_STEP = 10;
|
|
627
|
+
const ARROWS = {
|
|
628
|
+
ArrowUp: [0, -1],
|
|
629
|
+
ArrowDown: [0, 1],
|
|
630
|
+
ArrowLeft: [-1, 0],
|
|
631
|
+
ArrowRight: [1, 0]
|
|
632
|
+
};
|
|
633
|
+
const FOCUSABLE = "a[href],button,input,select,textarea,summary,iframe,[contenteditable=\"\"],[contenteditable=\"true\"],[tabindex]:not([tabindex=\"-1\"])";
|
|
634
|
+
let OverlayMarker = null;
|
|
635
|
+
let OverlayBase = null;
|
|
636
|
+
/**
|
|
637
|
+
* `google.maps.OverlayView` only exists once the API is loaded, so the class is created lazily.
|
|
638
|
+
* One instance per marker, created once: props changes go through the setters.
|
|
639
|
+
*/
|
|
640
|
+
const getOverlayMarkerClass = () => {
|
|
641
|
+
if (OverlayMarker && OverlayBase === google.maps.OverlayView) return OverlayMarker;
|
|
642
|
+
class GoogleMapsReactOverlayMarker extends google.maps.OverlayView {
|
|
643
|
+
container;
|
|
644
|
+
getHandlers;
|
|
645
|
+
position = {
|
|
646
|
+
lat: 0,
|
|
647
|
+
lng: 0
|
|
648
|
+
};
|
|
649
|
+
anchor = {
|
|
650
|
+
x: .5,
|
|
651
|
+
y: .5
|
|
652
|
+
};
|
|
653
|
+
draggable = false;
|
|
654
|
+
drag = null;
|
|
655
|
+
/** Position before a keyboard drag, restored by Escape. */
|
|
656
|
+
keyboardDrag = null;
|
|
657
|
+
suppressClick = false;
|
|
658
|
+
clickable = false;
|
|
659
|
+
title;
|
|
660
|
+
/** Watches the content of interactive markers for focusable elements of their own. */
|
|
661
|
+
contentObserver = null;
|
|
662
|
+
constructor({ map, container, getHandlers }) {
|
|
663
|
+
super();
|
|
664
|
+
this.container = container;
|
|
665
|
+
this.getHandlers = getHandlers;
|
|
666
|
+
container.style.position = "absolute";
|
|
667
|
+
container.style.left = "0";
|
|
668
|
+
container.style.top = "0";
|
|
669
|
+
google.maps.OverlayView.preventMapHitsFrom(container);
|
|
670
|
+
container.addEventListener("pointerdown", this.handlePointerDown);
|
|
671
|
+
container.addEventListener("pointermove", this.handlePointerMove);
|
|
672
|
+
container.addEventListener("pointerup", this.handlePointerUp);
|
|
673
|
+
container.addEventListener("pointercancel", this.handlePointerUp);
|
|
674
|
+
container.addEventListener("click", this.handleClick, true);
|
|
675
|
+
container.addEventListener("mousedown", this.stopWhileDraggable);
|
|
676
|
+
container.addEventListener("touchstart", this.stopWhileDraggable, { passive: true });
|
|
677
|
+
container.addEventListener("keydown", this.handleKeyDown);
|
|
678
|
+
container.addEventListener("keyup", this.handleKeyUp);
|
|
679
|
+
container.addEventListener("blur", this.handleBlur);
|
|
680
|
+
ensureMarkerStyles();
|
|
681
|
+
this.setMap(map);
|
|
682
|
+
}
|
|
683
|
+
onAdd() {
|
|
684
|
+
this.getPanes()?.overlayMouseTarget.appendChild(this.container);
|
|
685
|
+
}
|
|
686
|
+
draw() {
|
|
687
|
+
const point = this.getProjection()?.fromLatLngToDivPixel(this.position);
|
|
688
|
+
if (!point) return;
|
|
689
|
+
this.container.style.transform = `translate(${Math.round(point.x)}px, ${Math.round(point.y)}px) translate(${-this.anchor.x * 100}%, ${-this.anchor.y * 100}%)`;
|
|
690
|
+
}
|
|
691
|
+
onRemove() {
|
|
692
|
+
this.container.remove();
|
|
693
|
+
}
|
|
694
|
+
setPosition(position) {
|
|
695
|
+
if (this.drag || this.keyboardDrag) return;
|
|
696
|
+
this.position = position;
|
|
697
|
+
this.draw();
|
|
698
|
+
}
|
|
699
|
+
setAnchor(anchor) {
|
|
700
|
+
this.anchor = anchor;
|
|
701
|
+
this.draw();
|
|
702
|
+
}
|
|
703
|
+
setClassName(className) {
|
|
704
|
+
this.container.className = className ?? "";
|
|
705
|
+
}
|
|
706
|
+
setZIndex(zIndex) {
|
|
707
|
+
this.container.style.zIndex = zIndex === void 0 ? "" : String(zIndex);
|
|
708
|
+
}
|
|
709
|
+
setDraggable(draggable) {
|
|
710
|
+
this.draggable = draggable;
|
|
711
|
+
this.container.style.cursor = draggable ? "grab" : "";
|
|
712
|
+
this.container.style.touchAction = draggable ? "none" : "";
|
|
713
|
+
this.container.style.userSelect = draggable ? "none" : "";
|
|
714
|
+
if (!draggable && this.keyboardDrag) this.endKeyboardDrag(new KeyboardEvent("keydown"), false);
|
|
715
|
+
this.updateAccessibility();
|
|
716
|
+
}
|
|
717
|
+
setTitle(title) {
|
|
718
|
+
this.title = title;
|
|
719
|
+
if (title) this.container.title = title;
|
|
720
|
+
else this.container.removeAttribute("title");
|
|
721
|
+
this.updateAccessibility();
|
|
722
|
+
}
|
|
723
|
+
setClickable(clickable) {
|
|
724
|
+
this.clickable = clickable;
|
|
725
|
+
this.updateAccessibility();
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Clickable or draggable markers are buttons in the tab order, named by `title`, like
|
|
729
|
+
* Google's advanced markers. Other markers with a title are named groups; the rest stay plain.
|
|
730
|
+
*/
|
|
731
|
+
updateAccessibility() {
|
|
732
|
+
const { container, title } = this;
|
|
733
|
+
const watch = this.clickable || this.draggable;
|
|
734
|
+
if (watch && !this.contentObserver && typeof MutationObserver !== "undefined") {
|
|
735
|
+
this.contentObserver = new MutationObserver(() => this.updateAccessibility());
|
|
736
|
+
this.contentObserver.observe(container, {
|
|
737
|
+
childList: true,
|
|
738
|
+
subtree: true
|
|
739
|
+
});
|
|
740
|
+
} else if (!watch && this.contentObserver) {
|
|
741
|
+
this.contentObserver.disconnect();
|
|
742
|
+
this.contentObserver = null;
|
|
743
|
+
}
|
|
744
|
+
const interactive = this.draggable || this.clickable && !container.querySelector(FOCUSABLE);
|
|
745
|
+
const set = (name, value) => {
|
|
746
|
+
if (value === null) container.removeAttribute(name);
|
|
747
|
+
else container.setAttribute(name, value);
|
|
748
|
+
};
|
|
749
|
+
set("role", interactive ? "button" : title ? "group" : null);
|
|
750
|
+
set("tabindex", interactive ? "0" : null);
|
|
751
|
+
set("aria-label", title ?? null);
|
|
752
|
+
set("aria-describedby", this.draggable ? dragHelpId() : null);
|
|
753
|
+
set("aria-roledescription", this.draggable ? "draggable marker" : null);
|
|
754
|
+
}
|
|
755
|
+
setCollisionBehavior() {}
|
|
756
|
+
destroy() {
|
|
757
|
+
const { container } = this;
|
|
758
|
+
container.removeEventListener("pointerdown", this.handlePointerDown);
|
|
759
|
+
container.removeEventListener("pointermove", this.handlePointerMove);
|
|
760
|
+
container.removeEventListener("pointerup", this.handlePointerUp);
|
|
761
|
+
container.removeEventListener("pointercancel", this.handlePointerUp);
|
|
762
|
+
container.removeEventListener("click", this.handleClick, true);
|
|
763
|
+
container.removeEventListener("mousedown", this.stopWhileDraggable);
|
|
764
|
+
container.removeEventListener("touchstart", this.stopWhileDraggable);
|
|
765
|
+
container.removeEventListener("keydown", this.handleKeyDown);
|
|
766
|
+
container.removeEventListener("keyup", this.handleKeyUp);
|
|
767
|
+
container.removeEventListener("blur", this.handleBlur);
|
|
768
|
+
this.contentObserver?.disconnect();
|
|
769
|
+
this.contentObserver = null;
|
|
770
|
+
this.setMap(null);
|
|
771
|
+
}
|
|
772
|
+
stopWhileDraggable = (event) => {
|
|
773
|
+
if (this.draggable) event.stopPropagation();
|
|
774
|
+
};
|
|
775
|
+
handlePointerDown = (event) => {
|
|
776
|
+
this.suppressClick = false;
|
|
777
|
+
if (!this.draggable || event.button !== 0) return;
|
|
778
|
+
const origin = this.getProjection()?.fromLatLngToDivPixel(this.position);
|
|
779
|
+
if (!origin) return;
|
|
780
|
+
event.stopPropagation();
|
|
781
|
+
this.container.setPointerCapture?.(event.pointerId);
|
|
782
|
+
this.drag = {
|
|
783
|
+
pointerId: event.pointerId,
|
|
784
|
+
startX: event.clientX,
|
|
785
|
+
startY: event.clientY,
|
|
786
|
+
originX: origin.x,
|
|
787
|
+
originY: origin.y,
|
|
788
|
+
moved: false
|
|
789
|
+
};
|
|
790
|
+
};
|
|
791
|
+
handlePointerMove = (event) => {
|
|
792
|
+
const { drag } = this;
|
|
793
|
+
if (!drag || event.pointerId !== drag.pointerId) return;
|
|
794
|
+
const dx = event.clientX - drag.startX;
|
|
795
|
+
const dy = event.clientY - drag.startY;
|
|
796
|
+
if (!drag.moved) {
|
|
797
|
+
if (Math.hypot(dx, dy) < 3) return;
|
|
798
|
+
drag.moved = true;
|
|
799
|
+
this.container.style.cursor = "grabbing";
|
|
800
|
+
this.getHandlers().onDragStart?.(event, { latLng: this.position });
|
|
801
|
+
}
|
|
802
|
+
const latLng = this.getProjection()?.fromDivPixelToLatLng(new google.maps.Point(drag.originX + dx, drag.originY + dy));
|
|
803
|
+
if (!latLng) return;
|
|
804
|
+
this.position = {
|
|
805
|
+
lat: latLng.lat(),
|
|
806
|
+
lng: latLng.lng()
|
|
807
|
+
};
|
|
808
|
+
this.draw();
|
|
809
|
+
this.getHandlers().onDrag?.(event, { latLng: this.position });
|
|
810
|
+
};
|
|
811
|
+
handlePointerUp = (event) => {
|
|
812
|
+
const { drag } = this;
|
|
813
|
+
if (!drag || event.pointerId !== drag.pointerId) return;
|
|
814
|
+
this.drag = null;
|
|
815
|
+
if (this.container.hasPointerCapture?.(event.pointerId)) this.container.releasePointerCapture(event.pointerId);
|
|
816
|
+
if (!drag.moved) return;
|
|
817
|
+
this.container.style.cursor = this.draggable ? "grab" : "";
|
|
818
|
+
this.suppressClick = true;
|
|
819
|
+
setTimeout(() => {
|
|
820
|
+
this.suppressClick = false;
|
|
821
|
+
}, 0);
|
|
822
|
+
this.getHandlers().onDragEnd?.(event, { latLng: this.position });
|
|
823
|
+
};
|
|
824
|
+
/** Keys only act on the marker itself: buttons or inputs inside the content keep their own. */
|
|
825
|
+
handleKeyDown = (event) => {
|
|
826
|
+
if (event.target !== this.container) return;
|
|
827
|
+
const handled = () => {
|
|
828
|
+
event.preventDefault();
|
|
829
|
+
event.stopPropagation();
|
|
830
|
+
};
|
|
831
|
+
if (this.keyboardDrag) {
|
|
832
|
+
const arrow = ARROWS[event.key];
|
|
833
|
+
if (arrow) {
|
|
834
|
+
handled();
|
|
835
|
+
this.moveBy(arrow[0] * KEYBOARD_STEP * (event.shiftKey ? 5 : 1), arrow[1] * KEYBOARD_STEP * (event.shiftKey ? 5 : 1), event);
|
|
836
|
+
} else if (event.key === "Enter" || event.key === " ") {
|
|
837
|
+
handled();
|
|
838
|
+
this.endKeyboardDrag(event, true);
|
|
839
|
+
} else if (event.key === "Escape") {
|
|
840
|
+
handled();
|
|
841
|
+
this.endKeyboardDrag(event, false);
|
|
842
|
+
}
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
if (event.key === "Enter" && event.altKey && this.draggable) {
|
|
846
|
+
handled();
|
|
847
|
+
this.keyboardDrag = this.position;
|
|
848
|
+
this.container.style.cursor = "grabbing";
|
|
849
|
+
this.getHandlers().onDragStart?.(event, { latLng: this.position });
|
|
850
|
+
announce("Marker grabbed. Use the arrow keys to move it, Enter to drop, Escape to cancel.");
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
if (!this.clickable) return;
|
|
854
|
+
if (event.key === "Enter") {
|
|
855
|
+
handled();
|
|
856
|
+
this.getHandlers().onClick?.(event);
|
|
857
|
+
} else if (event.key === " ") handled();
|
|
858
|
+
};
|
|
859
|
+
handleKeyUp = (event) => {
|
|
860
|
+
if (event.target !== this.container || event.key !== " " || this.keyboardDrag || !this.clickable) return;
|
|
861
|
+
event.preventDefault();
|
|
862
|
+
this.getHandlers().onClick?.(event);
|
|
863
|
+
};
|
|
864
|
+
/** Leaving the marker with Tab drops it where it is. */
|
|
865
|
+
handleBlur = (event) => {
|
|
866
|
+
if (this.keyboardDrag) this.endKeyboardDrag(event, true);
|
|
867
|
+
};
|
|
868
|
+
moveBy(dx, dy, event) {
|
|
869
|
+
const projection = this.getProjection();
|
|
870
|
+
const origin = projection?.fromLatLngToDivPixel(this.position);
|
|
871
|
+
if (!projection || !origin) return;
|
|
872
|
+
const latLng = projection.fromDivPixelToLatLng(new google.maps.Point(origin.x + dx, origin.y + dy));
|
|
873
|
+
if (!latLng) return;
|
|
874
|
+
this.position = {
|
|
875
|
+
lat: latLng.lat(),
|
|
876
|
+
lng: latLng.lng()
|
|
877
|
+
};
|
|
878
|
+
this.draw();
|
|
879
|
+
this.getHandlers().onDrag?.(event, { latLng: this.position });
|
|
880
|
+
}
|
|
881
|
+
endKeyboardDrag(event, drop) {
|
|
882
|
+
const start = this.keyboardDrag;
|
|
883
|
+
if (!start) return;
|
|
884
|
+
this.keyboardDrag = null;
|
|
885
|
+
this.container.style.cursor = this.draggable ? "grab" : "";
|
|
886
|
+
if (!drop) {
|
|
887
|
+
this.position = start;
|
|
888
|
+
this.draw();
|
|
889
|
+
}
|
|
890
|
+
this.getHandlers().onDragEnd?.(event, { latLng: this.position });
|
|
891
|
+
announce(drop ? "Marker dropped." : "Drag cancelled.");
|
|
892
|
+
}
|
|
893
|
+
handleClick = (event) => {
|
|
894
|
+
if (this.suppressClick) {
|
|
895
|
+
this.suppressClick = false;
|
|
896
|
+
event.stopImmediatePropagation();
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
this.getHandlers().onClick?.(event);
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
OverlayMarker = GoogleMapsReactOverlayMarker;
|
|
903
|
+
OverlayBase = google.maps.OverlayView;
|
|
904
|
+
return OverlayMarker;
|
|
905
|
+
};
|
|
906
|
+
const createOverlayMarker = (init) => new (getOverlayMarkerClass())(init);
|
|
907
|
+
// src/marker.tsx
|
|
908
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
909
|
+
let warnedAboutTitle = false;
|
|
910
|
+
/**
|
|
911
|
+
* Renders any React content at `lat`/`lng`. Must be inside `<GoogleMap>`.
|
|
912
|
+
* Renders nothing on the server and until the map is ready.
|
|
913
|
+
*/
|
|
914
|
+
const Marker = (props) => {
|
|
915
|
+
const { lat, lng, anchor, zIndex, draggable = false, title, className, collisionBehavior, onClick, children } = props;
|
|
916
|
+
const { map, renderer } = useGoogleMapContext("Marker");
|
|
917
|
+
const latest = useLatest(props);
|
|
918
|
+
const handleRef = useRef(null);
|
|
919
|
+
const [container, setContainer] = useState(null);
|
|
920
|
+
useEffect(() => {
|
|
921
|
+
if (!map || !renderer) return void 0;
|
|
922
|
+
const element = document.createElement("div");
|
|
923
|
+
element.setAttribute(MARKER_ATTRIBUTE, "");
|
|
924
|
+
const init = {
|
|
925
|
+
map,
|
|
926
|
+
container: element,
|
|
927
|
+
getHandlers: () => latest.current
|
|
928
|
+
};
|
|
929
|
+
const handle = renderer === "advanced" ? createAdvancedMarker(init) : createOverlayMarker(init);
|
|
930
|
+
handleRef.current = handle;
|
|
931
|
+
setContainer(element);
|
|
932
|
+
return () => {
|
|
933
|
+
handle.destroy();
|
|
934
|
+
handleRef.current = null;
|
|
935
|
+
setContainer(null);
|
|
936
|
+
};
|
|
937
|
+
}, [
|
|
938
|
+
map,
|
|
939
|
+
renderer,
|
|
940
|
+
latest
|
|
941
|
+
]);
|
|
942
|
+
useEffect(() => {
|
|
943
|
+
handleRef.current?.setPosition({
|
|
944
|
+
lat,
|
|
945
|
+
lng
|
|
946
|
+
});
|
|
947
|
+
}, [
|
|
948
|
+
container,
|
|
949
|
+
lat,
|
|
950
|
+
lng
|
|
951
|
+
]);
|
|
952
|
+
const anchorId = anchorKey(anchor);
|
|
953
|
+
useEffect(() => {
|
|
954
|
+
handleRef.current?.setAnchor(resolveAnchor(anchor));
|
|
955
|
+
}, [container, anchorId]);
|
|
956
|
+
useEffect(() => {
|
|
957
|
+
handleRef.current?.setZIndex(zIndex);
|
|
958
|
+
}, [container, zIndex]);
|
|
959
|
+
useEffect(() => {
|
|
960
|
+
handleRef.current?.setDraggable(draggable);
|
|
961
|
+
}, [container, draggable]);
|
|
962
|
+
useEffect(() => {
|
|
963
|
+
handleRef.current?.setTitle(title);
|
|
964
|
+
}, [container, title]);
|
|
965
|
+
const clickable = Boolean(onClick);
|
|
966
|
+
useEffect(() => {
|
|
967
|
+
handleRef.current?.setClickable(clickable);
|
|
968
|
+
}, [container, clickable]);
|
|
969
|
+
useEffect(() => {
|
|
970
|
+
if (!isDev || warnedAboutTitle || !container || title || !(clickable || draggable)) return;
|
|
971
|
+
warnedAboutTitle = true;
|
|
972
|
+
console.warn("[google-maps-react-markers] A clickable or draggable <Marker> has no `title`. Screen readers announce it as an unnamed button: pass a short title.");
|
|
973
|
+
}, [
|
|
974
|
+
container,
|
|
975
|
+
title,
|
|
976
|
+
clickable,
|
|
977
|
+
draggable
|
|
978
|
+
]);
|
|
979
|
+
useEffect(() => {
|
|
980
|
+
handleRef.current?.setCollisionBehavior(collisionBehavior);
|
|
981
|
+
}, [container, collisionBehavior]);
|
|
982
|
+
useEffect(() => {
|
|
983
|
+
handleRef.current?.setClassName(className);
|
|
984
|
+
}, [container, className]);
|
|
985
|
+
return container ? createPortal(children, container) : null;
|
|
986
|
+
};
|
|
987
|
+
export { GoogleMap, MapControl, Marker, useGoogleMapsStatus, useMap, useMapsLibrary };
|
|
988
|
+
|
|
989
|
+
//# sourceMappingURL=index.js.map
|