Introducing a new way to review your PRs.

Read the announcement

visgl / react-google-maps

React components and hooks for the Google Maps JavaScript API

View on GitHub

19 documents · Updated Nov 2, 2026

docs / map-runtime

2D map runtime and camera synchronization

The Map component is the runtime bridge between React and a google.maps.Map instance. It waits for the Maps JavaScript API, creates or reuses an instance, keeps its view aligned with React props, and turns Maps events into React callbacks. The component also makes the active map available to child features and to code that needs direct map access.

Runtime boundary

API readiness and map ownership

Map runs inside an APIProvider, which owns API loading and the registry of active map instances. The map waits until the API and its container are ready before it creates a Google Maps instance. Without the provider boundary, the component fails because it has no API status or map registry.

References: src/…/index.tsx• L119-132 src/…/use-map-instance.ts• L53-65 src/…/api-provider.tsx• L21-36

Map container

The component supplies the DOM container that holds the Google map and renders its children only after an instance exists. It gives the container a full-size default so the parent layout can define the visible map area. An authentication failure replaces the map view with an error message.

References: src/…/index.tsx• L202-239 src/…/auth-failure-message.tsx• L1-100

Map instance lifecycle

Instance creation and registration

The runtime combines the initial map options with the initial camera values, then creates one google.maps.Map for the component. It registers that instance in the provider under the component id; a missing id uses the default registry entry. The instance is removed from the registry when the component unmounts.

The map needs a center and zoom in either controlled or default form. defaultBounds is an alternative that sets the first visible region. If no view is supplied, the runtime still creates the map and fits a world-sized region so the container can render.

References: src/…/use-map-instance.ts• L67-117 src/…/use-map-instance.ts• L135-203 src/…/use-map-instance.ts• L214-234

Instance reuse

reuseMaps lets the runtime keep map instances for later mounts instead of creating a new Google map each time. A cached instance is eligible only when its map ID, rendering type, and color scheme match the new component. The runtime restores the instance into the new container and reapplies current options.

This feature reduces repeated map-view creation when screens mount and unmount. It is best suited to repeated use of the same map setup because changing option sets across reuse can leave state that the Maps API does not reset automatically.

References: src/…/use-map-instance.ts• L14-24 src/…/use-map-instance.ts• L142-189 src/…/use-map-instance.ts• L221-232

Map configuration

Initial map settings

The runtime passes the initial Google Maps options to the constructor. Map ID, color scheme, and rendering type define the kind of map that is created, so changing one of these values creates a new instance. Usage attribution from the provider is combined with any IDs supplied to the map.

References: src/…/use-map-instance.ts• L104-117 src/…/use-map-instance.ts• L181-189 src/…/api-provider.tsx• L402-409

Runtime map options

Map behavior and appearance options are exposed as React props. Changes to options such as controls, gestures, map type, limits, styles, and interaction settings are applied to the existing Google map without replacing it. Camera props follow a separate synchronization path because the camera changes often during user interaction.

References: src/…/use-map-options.ts• L4-40 src/…/use-map-options.ts• L49-73

Camera synchronization

Camera modes

The camera is the visible map view: center, zoom, heading, and tilt. Each value can be controlled by a live prop or initialized with a default* prop. Default values set the starting view, while live values keep the map aligned with application state.

In externally controlled mode, the application is the only source of camera changes. The runtime disables map input and restores the requested camera when a map event tries to move it. This mode is useful when another renderer or state system owns navigation; map controls and instance access describes related control boundaries.

References: src/…/index.tsx• L39-44 src/…/index.tsx• L95-115 src/…/index.tsx• L136-200

Camera state tracking

The runtime records the last valid camera state reported by the map. On each React render it compares that state with the requested camera props and moves only the values that differ. The comparison accepts only finite center coordinates, zoom, heading, and tilt values. This prevents invalid input from moving the map and avoids feeding unchanged camera values back into Google Maps after an interaction.

The bounds_changed event updates the tracked state and schedules a React update. The synchronization effect then applies only changed values with moveCamera and records those values at once. This immediate update covers renders that occur before the map emits its next event. The result lets the application accept a new view or move the map back to the current props without a feedback loop.

References: src/…/use-tracked-camera-state-ref.ts• L4-9 src/…/use-tracked-camera-state-ref.ts• L53-77 src/…/use-map-camera-params.ts• L11-77

Camera synchronization flow

This flow shows how application camera props, map actions, and validated map events stay aligned.

flowchart LR
  P[React camera props] --> V[Finite camera values]
  V --> C[Camera comparison]
  C -->|changed values| M[Google Maps camera]
  U[User or external map action] --> M
  M --> E[bounds_changed]
  E --> S[Tracked camera state]
  S --> R[React camera callback]
  R --> P

References: src/…/use-map-camera-params.ts• L11-77 src/…/use-tracked-camera-state-ref.ts• L53-71 src/…/use-map-events.ts• L95-122

React event bridge

Event subscriptions

Map event props provide a React-shaped interface over Google Maps events. The runtime subscribes when a map and handler exist, passes a wrapped event to the handler, and removes the subscription when either one changes or the component unmounts.

The bridge covers camera, pointer, map-load, and map-configuration events. onCameraChanged is the broad camera callback and follows bounds_changed, which fires whenever the visible camera changes.

References: src/…/use-map-events.ts• L6-36 src/…/use-map-events.ts• L42-73 src/…/use-map-events.ts• L143-194

Event payloads

Every callback receives the map instance and an event detail. Camera events include the current center, bounds, zoom, heading, and tilt. Mouse events add the map location, place ID when available, and a way to stop the source event.

The bridge rejects camera events when the Maps API cannot provide a valid camera value. This keeps invalid data from reaching application state or overwriting the last known camera.

References: src/…/use-map-events.ts• L76-141 src/…/use-map-events.ts• L196-217

Map instance access

Context and lookup

After creation, Map provides its instance to descendants through an internal context and registers it with APIProvider. The useMap hook first resolves an explicit ID, then the nearest parent map, and finally the provider's default map. It returns null when no matching instance exists.

Use useMap for direct map access. Use map controls and instance access for the higher-level rules that govern controls, multiple maps, and child components.

References: src/…/index.tsx• L26-30 src/…/index.tsx• L215-237 src/…/use-map.ts• L7-36

The runtime is the base layer for features that draw on or act on a map instance. Map overlays build on the same map lifecycle, while React integration utilities provide shared patterns for binding API objects and events to React state.

References: src/…/index.tsx• L130-137 src/…/use-maps-event-listener.ts• L11-25 src/…/use-prop-binding.ts• L7-38

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to the map registry must preserve the default ID and explicit-ID lookup rules used by useMap.
  • Changes to camera event names must keep onCameraChanged, tracked camera state, and camera prop reconciliation on the same source event.
  • Changes to camera input validation must keep non-finite values out of both moveCamera calls and application camera state.
  • Changes to instance identity must update both the constructor boundary and the reuseMaps cache key. These values cannot be changed on an existing Google map.
  • Changes to event payload validation must keep invalid Maps API values out of application camera state.

Important failure modes

TriggerConsequenceGuard
Map renders outside APIProviderThe map cannot load or register its instance.Keep every Map below one APIProvider.
No center and zoom, and no defaultBoundsThe map has no intended initial view.Provide both live or default center and zoom, or provide bounds.
A camera prop contains a non-finite valueThat camera field is not applied.Pass finite center coordinates, zoom, heading, and tilt values.
A camera event contains an invalid center, bounds, or zoomApplication state must not receive a corrupt camera payload.The event bridge drops the event and the tracker keeps its last valid state.
A cached map has no usable map DOM nodeReuse cannot restore the map into the new container.Discard the broken cache entry and create a new instance.
id values collide in one providerA later map replaces the earlier registry entry.Give each map a unique ID when more than one map is present.

Key files

FileWhy to read it
src/…/index.tsxMain component boundary, context, camera modes, and container behavior.
src/…/use-map-instance.tsMap creation, provider registration, default view, and reuse lifecycle.
src/…/use-map-camera-params.tsCamera prop validation and reconciliation from React props to the map.
src/…/use-tracked-camera-state-ref.tsLast-known camera state used to avoid feedback loops.
src/…/use-map-events.tsMapping from Maps events to React callbacks and event payloads.
src/…/use-map-options.tsRuntime application of non-camera map options.
src/…/api-provider.tsxAPI readiness and the registry shared by map instances.
src/…/use-map.tsPublic lookup rules for one or many map instances.