MCPcopy
hub / github.com/visgl/react-google-maps / useMapInstance

Function useMapInstance

src/components/map/use-map-instance.ts:53–249  ·  view source on GitHub ↗
(
  props: MapProps,
  context: APIProviderContextValue
)

Source from the content-addressed store, hash-verified

51 * @internal
52 */
53export function useMapInstance(
54 props: MapProps,
55 context: APIProviderContextValue
56): readonly [
57 map: google.maps.Map | null,
58 containerRef: Ref<HTMLDivElement>,
59 cameraStateRef: CameraStateRef
60] {
61 const apiIsLoaded = useApiIsLoaded();
62 const [map, setMap] = useState<google.maps.Map | null>(null);
63 const [container, containerRef] = useCallbackRef<HTMLDivElement>();
64
65 const cameraStateRef = useTrackedCameraStateRef(map);
66
67 const {
68 id,
69 defaultBounds,
70 defaultCenter,
71 defaultZoom,
72 defaultHeading,
73 defaultTilt,
74 reuseMaps,
75 renderingType,
76 colorScheme,
77
78 ...mapOptions
79 } = props;
80
81 const hasZoom = props.zoom !== undefined || props.defaultZoom !== undefined;
82 const hasCenter =
83 props.center !== undefined || props.defaultCenter !== undefined;
84
85 if (!defaultBounds && (!hasZoom || !hasCenter)) {
86 console.warn(
87 '<Map> component is missing configuration. ' +
88 'You have to provide zoom and center (via the `zoom`/`defaultZoom` and ' +
89 '`center`/`defaultCenter` props) or specify the region to show using ' +
90 '`defaultBounds`. See ' +
91 'https://visgl.github.io/react-google-maps/docs/api-reference/components/map#required'
92 );
93 }
94
95 // apply default camera props if available and not overwritten by controlled props
96 if (!mapOptions.center && defaultCenter) mapOptions.center = defaultCenter;
97 if (!mapOptions.zoom && Number.isFinite(defaultZoom))
98 mapOptions.zoom = defaultZoom;
99 if (!mapOptions.heading && Number.isFinite(defaultHeading))
100 mapOptions.heading = defaultHeading;
101 if (!mapOptions.tilt && Number.isFinite(defaultTilt))
102 mapOptions.tilt = defaultTilt;
103
104 // Handle internalUsageAttributionIds
105 const customIds = mapOptions.internalUsageAttributionIds;
106
107 if (customIds == null) {
108 // Not specified - use context default (which may be null if disabled)
109 mapOptions.internalUsageAttributionIds =
110 context.internalUsageAttributionIds;

Callers 1

MapFunction · 0.90

Calls 9

useApiIsLoadedFunction · 0.90
useCallbackRefFunction · 0.90
useTrackedCameraStateRefFunction · 0.90
addMapInstanceFunction · 0.85
removeMapInstanceFunction · 0.85
hasMethod · 0.80
popMethod · 0.80
appendChildMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected