3D map access hook
The useMap3D hook gives a consumer access to the active Google Maps 3D map element. It connects React components to the map supplied by the nearest <Map3D> or to a named map managed by <APIProvider>, so other components can read or control the map without passing it through props. See the 3D maps page for the map lifecycle and 3D scene elements for components that use the map.
Access model
useMap3D returns a google.maps.maps3d.Map3DElement or null. The hook uses two context layers: <Map3D> supplies its own map to descendants, while <APIProvider> keeps the named map instances that support access from elsewhere in the provider tree.
Map selection
The hook selects a map in a fixed order. This lets a component stay local to its parent map by default while still allowing a control or other shared component to target a specific map.
useMap3D(id)returns the map registered with the suppliedid.useMap3D()returns the nearest parent<Map3D>map.- If no parent map exists,
useMap3D()returns the provider'sdefaultmap. - If no matching map exists, the hook returns
null.
flowchart TD
A[Component calls useMap3D] --> B{An id is supplied?}
B -->|Yes| C[Look up the named map in APIProvider]
B -->|No| D{A parent Map3D provides a map?}
D -->|Yes| E[Return the parent map]
D -->|No| F[Look up the default map in APIProvider]
C --> G{Map found?}
F --> G
G -->|Yes| H[Return Map3DElement]
G -->|No| I[Return null]
References: src/…/use-map-3d.ts• L7-15 src/…/use-map-3d.ts• L33-56
Context requirements
The hook must run below <APIProvider>. That provider loads the Maps API and owns the shared map registry. A component below <Map3D> also receives the nearest map through the 3D map context, but <Map3D> itself must be below <APIProvider>.
When the API provider is missing, the hook logs an error and returns null rather than producing a map. Consumers must therefore treat the return value as optional and must not call map methods until a map is available.
References: src/…/use-map-3d.ts• L36-47 src/…/api-provider.tsx• L21-45 src/…/index.tsx• L167-178
Map lifecycle
The map becomes available only after <Map3D> creates its underlying map element. Until then, the hook can return null. When the map is ready, <Map3D> registers it with the provider and supplies it to its children through the local context.
Local map access
Components rendered inside <Map3D> should normally call useMap3D() without an id. This follows the nearest map and keeps scene controls and 3D elements attached to the map that contains them. The local context is present only when the map is ready, so child components do not receive a partially created map.
References: src/…/index.tsx• L180-197 src/…/index.tsx• L226-255 src/…/use-map-3d.ts• L50-55
Named map access
Applications with more than one 3D map in one provider should give each map a distinct id and pass that same value to useMap3D(id). The id selects the provider registry entry and does not depend on which map is the nearest React parent. A map is removed from the registry when its <Map3D> unmounts or changes its id.
References: src/…/index.tsx• L94-105 src/…/index.tsx• L187-197 src/…/api-provider.tsx• L165-190 src/…/use-map-3d.ts• L50-55
Consumer use
The hook returns the underlying map element, not a separate wrapper object. Consumers can use that element for map-specific actions or pass it to code that works with the Google Maps 3D API. React components that only render scene content can usually rely on their parent <Map3D> instead of retrieving the map directly.
import {useMap3D} from '@vis.gl/react-google-maps/3d';
function CameraControl() {
const map3d = useMap3D();
if (!map3d) return null;
return (
<button
onClick={() =>
map3d.flyCameraTo({
endCamera: {
center: {lat: 37.7749, lng: -122.4194, altitude: 1000},
tilt: 45,
heading: 0,
range: 5000
}
})
}>
Fly to map
</button>
);
}
For a named map, the consumer passes the map id instead:
function MainMapControl() {
const map3d = useMap3D('main-map');
return map3d ? <button onClick={() => map3d.stopCameraAnimation()}>Stop</button> : null;
}
The 3D entry point exports the hook for direct consumer use. The older package export still exposes the same hook for compatibility, but new code should use the dedicated 3D entry point.
References: src/…/index.ts• L5-15 src/…/use-map-3d.ts• L17-31 src/deprecated-3d-exports.ts• L30-33 src/deprecated-3d-exports.ts• L117-119
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the selection order in
useMap3Daligned with the way<Map3D>registers local and named instances. - Keep the
Map3DElement | nullcontract aligned across the hook, 3D context, and provider registry. - Keep the dedicated 3D export aligned with compatibility exports when the hook's public path changes.
- Preserve id cleanup so an unmounted map cannot remain selectable from a shared control.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A consumer calls useMap3D outside <APIProvider> | The hook logs an error and returns null. | Place the consumer below the provider. |
A named id does not match a mounted <Map3D> | The hook returns null even when another 3D map exists. | Use the exact id shared by the map and the consumer. |
| A consumer uses the map before it is ready | Map actions have no target. | Check for null before calling map methods. |
| Multiple maps share an id | A shared lookup cannot identify the intended map reliably. | Give each map a unique id within its provider. |
Key files
| File | Why to read it |
|---|---|
| src/…/use-map-3d.ts | Defines the public hook and its map selection order. |
| src/…/index.tsx | Shows how <Map3D> creates the local context and registers map instances. |
| src/…/api-provider.tsx | Defines the provider context and the shared 3D map registry. |
| src/…/index.ts | Defines the dedicated public entry point for 3D components and hooks. |
| src/…/use-map-3d.test.tsx | Summarizes the expected results for local maps, named maps, missing maps, and missing providers. |