2D map overlays and geometry
The 2D overlay components connect React props and children to Google Maps objects. They add markers, information windows, pins, circles, lines, polygons, and rectangles to a map, then keep those objects aligned with application state and user actions.
Overlay model
React and Google Maps objects
Each wrapper has two parts: a React-facing component and a Google Maps object. The wrapper waits for its map or required Maps library, creates the object, places it on the map, and removes it when the component leaves the tree. Later prop changes update the existing object instead of replacing the whole overlay.
The main relationship is shown below.
flowchart LR
P[React props and children] --> W[Overlay wrapper]
W -->|creates and updates| G[Google Maps object]
G -->|events and edits| W
W -->|callbacks and state changes| A[Application]
W -->|cleanup| M[Map]
This model lets React own intent while Google Maps owns the live map object. Event props send map actions back to the application, and refs provide access when another component must use the live object.
References: src/…/marker.tsx• L27-65 src/…/circle.tsx• L47-105 src/…/use-map.ts• L12-36
Map context and library readiness
Overlay wrappers use the nearest map supplied by the map runtime. Marker presentations that depend on the marker library and geometry features that depend on the geometry library wait until that library is ready before they create or update their Maps objects.
An overlay must therefore be rendered inside a usable map context. The broader provider, map, and API loading flow is described in map-runtime.md.
References: src/…/use-map.ts• L12-36 src/…/use-maps-library.ts• L8-28 src/…/advanced-marker.tsx• L193-251 src/…/polyline.tsx• L70-75
Marker presentations
Marker components place a point on the map. They range from the basic Google Maps marker to an advanced marker with React content, while pins provide a ready-made marker appearance.
Standard markers
Marker represents the classic Google Maps marker. Its position and display options come from props, and its map events are exposed as React callbacks. The component also exposes the live marker through a ref, which allows an information window or another map feature to use it as an anchor.
Use markers-and-info-windows.md for the marker presentation choices and the relationship between markers and information windows.
References: src/…/marker.tsx• L13-25 src/…/marker.tsx• L67-113 src/…/marker.tsx• L121-138
Advanced markers
AdvancedMarker represents the newer marker model. It supports a default map pin or React children rendered as marker content, so an application can use a ready-made marker or supply its own HTML and CSS. Position, title, stacking order, collision behavior, anchoring, click behavior, and drag behavior stay tied to props.
The marker library must be available before the advanced marker can exist. A ref exposes the live advanced marker for use by an InfoWindow or other map code.
References: src/…/advanced-marker.tsx• L77-115 src/…/advanced-marker.tsx• L123-165 src/…/advanced-marker.tsx• L193-339
Pins and custom marker content
Pin is an appearance component for an advanced marker or another compatible marker surface. It selects the Maps pin form supported by the loaded API version and keeps the glyph props useful across old and new pin APIs. React children can also provide a custom glyph.
This separates marker position and interaction from marker appearance. Read the Pin API reference for the available visual options.
References: src/…/pin.tsx• L6-16 src/…/pin.tsx• L38-80 docs/…/pin.md• L1-19
Information windows
InfoWindow displays React content at a position or beside a marker. It opens when its map and content are ready, uses the supplied marker as its anchor when present, and reports map-driven closing through onClose so application state can remain accurate.
The component renders both body content and optional header content into the Maps information window. This makes the window a separate overlay that can follow a marker without becoming part of the marker itself. See markers-and-info-windows.md for the complete presentation flow.
References: src/…/info-window.tsx• L21-34 src/…/info-window.tsx• L63-125 src/…/info-window.tsx• L180-255
Geometric overlays
Geometric overlays describe areas and paths on the map rather than single points. The wrappers use the same object lifecycle as markers, but they also handle editable geometry and the difference between initial geometry and controlled geometry.
Shape families
The 2D geometry wrappers cover four common shape families:
| Component | Map feature | Main geometry value |
|---|---|---|
Circle | Round area | Center and radius |
Rectangle | Bounds area | North, south, east, and west bounds |
Polyline | Open path | Ordered path points |
Polygon | Closed area | One or more ordered paths |
Each shape also supports map interaction events. The line and polygon forms can use encoded paths when the application stores geometry in a compact string format. See geometric-overlays.md for shape-specific behavior.
References: src/…/circle.tsx• L30-43 src/…/rectangle.tsx• L31-40 src/…/polyline.tsx• L34-52 src/…/polygon.tsx• L36-54
Controlled and initial geometry
Shape wrappers support controlled geometry through props such as center, radius, bounds, path, and paths. They also support default* values for geometry that is set when the object starts but is then allowed to change through map interaction. Controlled values are compared with the current Maps value before an update is sent.
This gives applications a clear choice: keep geometry in React state when the map must follow that state, or provide an initial value when the map should own later edits.
References: src/…/circle.tsx• L139-155 src/…/rectangle.tsx• L124-131 src/…/polyline.tsx• L190-210 src/…/polygon.tsx• L243-275
Editable geometry events
An edit callback makes a shape interactive and returns the current geometry after a drag or vertex edit. The wrappers listen to both whole-shape movement and the Maps editable path changes, then avoid treating their own controlled updates as user edits. This prevents a prop update from causing a second state update in the application.
The resulting flow is:
- The user drags a shape or changes its vertices.
- Google Maps changes the live geometry.
- The wrapper reads the new center, radius, bounds, path, or paths.
- The callback sends that value to application state.
References: src/…/circle.tsx• L107-131 src/…/rectangle.tsx• L102-116 src/…/polyline.tsx• L145-182 src/…/polygon.tsx• L159-235
Change checklist
When these files or sections are changed, remember to consider these:
- Keep marker refs and information-window anchors compatible when marker object types or anchor timing change.
- Keep
default*geometry props distinct from controlled geometry props so an initial value does not overwrite later user edits. - Keep encoded path handling aligned with the geometry library because encoded values take precedence over ordinary path values.
- Keep inferred clickable, draggable, and editable behavior aligned with the event callback contracts for every shape.
- Keep marker pin prop mapping compatible with both supported Maps API pin forms.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An overlay renders without a usable map context | The Maps object cannot be placed on a map | Render overlays below Map and APIProvider |
| An advanced marker or pin renders before the marker library is ready | The marker or pin has no live Maps object | Let the wrapper wait for library readiness |
An InfoWindow anchor is removed or closed by the map | React state can still say that the window is open | Handle onClose and update application state |
| A controlled geometry value differs from a user edit | The next render replaces the map edit | Store the edit in application state before passing it back as the controlled value |
| A shape receives an encoded path without the geometry library | The encoded geometry cannot be decoded | Load the geometry library before relying on encoded paths |
Key files
| File | Why to read it |
|---|---|
| src/…/marker.tsx | Shows the basic marker lifecycle, events, position updates, and refs. |
| src/…/advanced-marker.tsx | Shows custom marker content, marker-library readiness, and marker anchoring. |
| src/…/info-window.tsx | Shows anchored and free information windows plus React content rendering. |
| src/…/pin.tsx | Shows version-aware pin appearance selection. |
| src/…/circle.tsx | Shows controlled center and radius synchronization. |
| src/…/polyline.tsx | Shows controlled paths, encoded paths, and editable line events. |
| src/…/polygon.tsx | Shows multi-path geometry and editable polygon events. |
| src/…/rectangle.tsx | Shows controlled bounds synchronization. |
| src/…/use-map.ts | Shows how overlays find their map instance. |
| src/…/use-maps-library.ts | Shows how overlays wait for Maps libraries. |
| src/…/set-map-object-options.ts | Shows the shared rule for applying only changed shape options. |