Map controls and map instance access
Map controls and access hooks connect application UI to the map runtime. MapControl places React content in the Maps JavaScript API control layout, while useMap and useMap3D let child components reach the right map instance by context or by name. See Map runtime for the wider map lifecycle.
Control attachment
Map control placement
MapControl gives application UI a position inside a 2D map. It creates one control container for its children, registers that container with the selected map position, and renders the React children into it. This lets custom UI use the map's own layout alongside built-in controls.
The position value uses the public ControlPosition constants. className and style apply to the wrapper that the map lays out, so changes to its size or placement can affect other controls at the same position.
When the control or its position changes, the old registration is removed. When the map is not available, the children remain outside the map control layout until a map instance can be resolved.
flowchart LR
UI[React control content] --> MC[MapControl]
MC --> C[Control container]
C --> P[Map control position]
P --> M[2D map UI]
References: src/…/map-control.tsx• L14-18 src/…/map-control.tsx• L58-99 docs/…/map-control.md• L3-10
Map instance access
Contextual and named 2D access
useMap provides a 2D google.maps.Map to components that need to act on a map. Without an ID, it first uses the nearest parent Map; if no parent exists, it uses the provider's default instance. With an ID, it looks up only that named instance.
The Map component supplies the contextual instance to its descendants. The APIProvider supplies the shared name-to-instance registry, which allows several maps to coexist in one provider. A lookup returns null when the requested instance does not exist or when the hook is outside an APIProvider.
flowchart TD
H[useMap(id?)] --> I{ID supplied?}
I -- Yes --> N[Named instance in APIProvider]
I -- No --> C[Nearest parent Map]
C -- None --> D[default instance in APIProvider]
N --> R[Map instance or null]
D --> R
References: src/…/use-map.ts• L7-36 src/…/index.tsx• L26-30 src/…/index.tsx• L71-75 src/…/index.tsx• L227-239
Contextual and named 3D access
useMap3D follows the same access model for a Map3DElement. An explicit ID selects a named 3D instance from the provider. Without an ID, the hook uses the nearest parent Map3D and then falls back to the provider's default instance.
This hook is useful for 3D child components that must follow their containing map without receiving a map reference as a prop. It returns null when no matching instance is ready or when the component is outside an APIProvider.
References: src/…/use-map-3d.ts• L7-15 src/…/use-map-3d.ts• L33-56 src/…/index.tsx• L62-73 src/…/index.tsx• L167-197 src/…/index.tsx• L226-255
Instance naming
Provider instance registry
APIProvider owns the shared registry for map instances. A map with no id uses the default name. A map with an id uses that name, so sibling maps in one provider can be addressed without relying on component nesting.
The name is a lookup key rather than a visual label. The map component registers its ready instance and removes that name when the component leaves the page. The same relationship applies to 3D map instances through the separate 3D registry.
References: src/…/api-provider.tsx• L21-35 src/…/api-provider.tsx• L139-190 src/…/api-provider.tsx• L415-467 src/…/use-map-instance.ts• L135-194 src/…/index.tsx• L178-197
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the
MapID contract aligned with theuseMaplookup rules. A change to the default name or explicit-name behavior can silently break controls and child components. - Keep
MapControlregistration and cleanup aligned with map replacement and position changes. A stale container can remain in a map control array or disappear from the intended position. - Keep the 2D and 3D access rules parallel when changing provider registration. They use separate registries and separate parent contexts.
- Keep public exports aligned when moving an access hook or control component. Consumers receive these APIs through the package entry point.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
MapControl renders without a resolved map | Its container is not registered with map controls | Render it under a ready Map inside APIProvider |
| A named hook ID does not match any registered map | useMap or useMap3D returns null | Use the exact id used by the target map |
A map-access hook runs outside APIProvider | The hook returns null and logs an error | Keep map-aware components inside one provider |
| Several maps share the provider default name | Name-based access cannot distinguish the maps | Give each map an explicit unique id |
| Control styling changes the wrapper size or placement | Neighboring controls can move in the Maps layout | Treat className and style as part of the map control layout |
Key files
| File | Why to read it |
|---|---|
| src/…/map-control.tsx | Defines how React content enters and leaves the 2D map control layout. |
| src/…/use-map.ts | Defines contextual and named 2D map lookup. |
| src/…/use-map-3d.ts | Defines contextual and named 3D map lookup. |
| src/…/index.tsx | Shows how a 2D map gives its instance to descendants. |
| src/…/index.tsx | Shows how a 3D map gives its instance to descendants and the provider registry. |
| src/…/api-provider.tsx | Defines the shared instance registries used by named lookups. |
| src/index.ts | Shows the public package exports for controls and map access. |