3D maps and rendering lifecycle
The Map3D component provides a React boundary for a Google Maps 3D map. It loads the 3D map element through APIProvider, keeps its camera and options aligned with React props, forwards map events, and makes the live map available to child components and application code.
Map3D component
Map3D is the main entry point for a 3D map view. It must run inside APIProvider, and its children render inside the ready Google Maps 3D element so scene elements can use the same map instance. See three-dimensional-scene-elements.md for the child elements that use this boundary.
The component accepts a render mode and camera settings. The camera is described by a geographic center with altitude plus range, heading, tilt, and roll. The render mode defaults to ROADMAP when no mode is given. A controlled value keeps the map aligned with React state; a default* value sets the initial camera state without later prop control.
Map instance lifecycle
The map instance lifecycle joins two readiness conditions: the maps3d library must be loaded and the gmp-map-3d custom element must be defined. Until both conditions are true, the component keeps its container in place without rendering the map element. Once ready, it creates the element, applies its initial camera and map options, and registers the instance with the surrounding APIProvider.
The lifecycle then has a clear ownership model:
APIProviderowns the set of registered 3D map instances.Map3Dregisters its instance under itsid, or underdefaultwhen no id is given.- Child content receives the nearest live instance through
GoogleMaps3DContext. - Unmounting the component removes its registration.
The id matters when one provider contains more than one 3D map. It gives public access code a stable way to select one map instead of relying on the default instance.
The following flow shows how loading and registration lead to a usable map and child scene.
flowchart TD
A[APIProvider loads maps3d library] --> B[Map3D waits for gmp-map-3d]
B --> C[Map3D creates map element]
C --> D[Initial camera and options apply]
D --> E[Instance registers with APIProvider]
E --> F[Child context exposes live instance]
E --> G[useMap3D selects instance]
C --> H[Unmount removes registration]
Camera synchronization
The camera synchronization layer supports both map interaction and React control. It tracks the last camera state from the map, compares that state with controlled props, and writes only changed values back to the map. This prevents map events from causing needless camera writes while still allowing application state to drive the view.
Camera changes produce per-property events for center, range, heading, tilt, and roll. The onCameraChanged callback combines those changes into one event with the current full camera state. An application can use that event to update controlled props after a user moves the camera.
The camera also supports imperative flight through the Map3D ref. The ref exposes the underlying map plus methods to fly around a center, fly to a destination, or stop an active animation.
Event synchronization
Map3D translates native Google Maps 3D events into React callback props. Camera callbacks include a normalized center and all camera values; click callbacks include the clicked position and an optional place id; steady-state callbacks report whether the map has settled. Animation-end and error callbacks expose the related map event.
The event layer attaches listeners when a map instance and callback are available. It removes those listeners when either changes or when the component unmounts. This keeps event delivery tied to the current map instance and avoids stale listeners after remounts.
Public 3D access
The public 3D entry point re-exports Map3D, its camera and event types, useMap3D, and the 3D scene components. The preferred import path is @vis.gl/react-google-maps/3d; the older 3D export surface remains as a deprecated compatibility path. The access rules are described in three-dimensional-map-access.md.
useMap3D() returns the map selected by its lookup scope:
- An explicit id selects that registered map from
APIProvider. - Without an id, a child component receives its nearest parent
Map3Dinstance. - Without a parent map, the hook returns the provider's
defaultinstance. - If no matching instance exists, it returns
null.
This access model connects map-wide controls with local scene elements. A child can work with its parent map without knowing an id, while an external control can select a named map when several maps share one provider.
Related 3D systems
The map boundary supplies the shared instance and camera lifecycle. Scene elements add content inside that boundary, while map access APIs let controls and other components read or act on the live instance. The general map runtime provides the provider and loading context that the 3D lifecycle depends on; see map-runtime.md.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
Map3Dregistration id rules aligned withuseMap3Dlookup priority and the provider's instance store. - Keep controlled camera props, camera-change event details, and the tracked camera state aligned so user movement does not fight React updates.
- Keep the
maps3dlibrary and custom-element readiness conditions aligned with when child context becomes usable. - Keep the
/3dexport surface and deprecated compatibility exports aligned when a public 3D entry point changes. - Keep the default
ROADMAPmode aligned between the publicMapModetype and the options passed to the live map element.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Map3D renders outside APIProvider | The component throws because it has no loading or instance registry context. | Place every Map3D below one APIProvider. |
| Multiple maps use the same id | A later registration replaces the earlier map in the provider registry. | Give each map a unique id when more than one map is present. |
A controlled camera does not consume onCameraChanged | User camera movement is overwritten by the old React prop values. | Update controlled camera state from the camera-change event. |
| Code requests an unknown map id | useMap3D returns null, so imperative work has no target. | Match the hook id to the Map3D id and handle the null state. |
Key files
| File | Why to read it |
|---|---|
| src/…/index.tsx | Defines the Map3D boundary, child context, registration, ref actions, and public prop model. |
| src/…/use-map-3d-instance.ts | Shows the readiness and initial instance lifecycle. |
| src/…/use-map-3d-camera-params.ts | Shows how controlled camera values reach the map. |
| src/…/use-map-3d-events.ts | Shows how native 3D events become React callbacks. |
| src/…/use-map-3d-options.ts | Shows which map options reach the live element and how the default render mode is applied. |
| src/…/use-map-3d.ts | Defines public map instance lookup behavior. |
| src/…/api-provider.tsx | Owns the 3D instance registry and API loading context. |
| src/…/index.ts | Defines the preferred public 3D export surface. |