Geometric overlays
Geometric overlays connect React props to Google Maps shapes that appear on a map. The Circle, Rectangle, Polygon, and Polyline wrappers cover area or draw lines while keeping their map objects, visual options, user events, and changing geometry in step with the React tree. See Map overlays for the wider overlay model.
Shape roles
Each wrapper represents one Google Maps shape and accepts the matching Maps option set, except that React owns the map attachment and geometry props.
| Wrapper | Geometry | Main controlled value | Initial-only value |
|---|---|---|---|
Circle | A center point with a radius in meters | center, radius | defaultCenter, defaultRadius |
Rectangle | North, south, east, and west bounds | bounds | defaultBounds |
Polygon | One path or several paths, including holes | paths | defaultPaths |
Polyline | One ordered line path | path | defaultPath |
The wrappers render no DOM content. They create or use a map shape and attach it to the nearest Map context. The shape is removed from the map when the wrapper leaves the React tree.
References: src/…/circle.tsx• L30-45 src/…/rectangle.tsx• L31-42 src/…/polygon.tsx• L36-56 src/…/polyline.tsx• L34-54
Shape options
The wrappers pass the Google Maps option types through as props. This gives all four shapes common display and behavior controls while keeping geometry in shape-specific props.
Display options
Circles, rectangles, and polygons support fill and outline settings. Polylines support outline settings and can place icons along the line. Polygon and polyline edges can use geodesic rendering.
fillColorandfillOpacityapply to filled shapes.strokeColor,strokeOpacity, andstrokeWeightcontrol outlines.iconsapplies to polylines.geodesicchanges how polygon and polyline edges follow the earth.
References: docs/…/circle.md• L118-127 docs/…/rectangle.md• L122-130 docs/…/polygon.md• L192-202 docs/…/polyline.md• L156-165
Behavior options
All four wrappers support clickable, draggable, editable, visible, and zIndex where the Google Maps shape supports them. A handler can infer the needed interaction mode, but an explicit option wins. For example, clickable={false} prevents click behavior even when onClick is present.
The wrappers infer these options from handlers when the option is not set:
| Handler | Inferred option | Shapes |
|---|---|---|
onClick | clickable: true | All |
onDrag, onDragStart, onDragEnd, or geometry-change handler | draggable: true | All |
| Geometry-change handler | editable: true | All |
References: src/…/circle.tsx• L67-76 src/…/rectangle.tsx• L63-72 src/…/polygon.tsx• L93-103 src/…/polyline.tsx• L79-89
Event bindings
The wrappers bind React callbacks to Google Maps events only while both the shape and callback exist. The shared event binding keeps the current callback without rebuilding the map listener for every render.
All shapes expose the same pointer and drag events:
onClickruns when the shape is clicked.onMouseOverandonMouseOutrun when the pointer enters or leaves the shape.onDragStart,onDrag, andonDragEndreport a shape drag.
Each shape also exposes a geometry-change callback. Circle reports onCenterChanged and onRadiusChanged. Rectangle reports onBoundsChanged. Polygon reports onPathsChanged. Polyline reports onPathChanged.
Geometry callbacks return the current map geometry. The polygon and polyline wrappers report edits to their vertex arrays as well as movement of the whole shape. This lets an application store the new geometry after a user edit.
References: src/…/use-maps-event-listener.ts• L7-25 src/…/circle.tsx• L107-131 src/…/rectangle.tsx• L102-116 src/…/polygon.tsx• L153-165 src/…/polygon.tsx• L167-235 src/…/polyline.tsx• L139-182
Geometry state
Controlled geometry makes React the source of truth. When a controlled value changes, the wrapper compares it with the map shape and updates the shape only when the geometry differs. default* values set the first geometry and then allow the map object to change without React writing it back.
Editable or draggable shapes need a two-way flow. The user changes the map object, the wrapper calls the geometry-change handler, and the application updates its controlled state. Without that state update, the next controlled render can restore the old geometry.
flowchart LR
P[Controlled geometry prop] --> S[Map shape]
S --> E[Edit or drag event]
E --> C[Geometry-change callback]
C --> A[Application state]
A --> P
Circle compares center and radius. Rectangle compares bounds. Polyline compares one path. Polygon accepts a flat path or nested paths and compares the normalized shape, so nested paths can represent holes.
References: src/…/circle.tsx• L139-155 src/…/rectangle.tsx• L124-131 src/…/polygon.tsx• L243-275 src/…/polyline.tsx• L190-210 docs/…/circle.md• L33-64 docs/…/rectangle.md• L37-79 docs/…/polygon.md• L40-83 docs/…/polyline.md• L38-73
Encoded geometry
Polyline accepts one encoded path through encodedPath. Polygon accepts encoded paths through encodedPaths. The wrappers decode these values into map paths and give encoded values precedence over path or paths when both are provided.
Encoded geometry needs the Google Maps geometry library. The wrapper requests that library when the prop is in use, then applies the decoded path to the shape.
References: src/…/polygon.tsx• L265-275 src/…/polyline.tsx• L202-210 src/…/use-maps-library.ts• L8-29 docs/…/polygon.md• L141-160 docs/…/polyline.md• L106-124
Existing shape instances
Polygon and Polyline can manage an existing Google Maps instance through the polygon or polyline prop. The wrapper still applies its geometry, options, and event handlers to that instance, then controls its map attachment for the lifetime of the wrapper.
This mode is useful when another part of an application creates the map object but React should own its visible state and events. Circle and Rectangle create their matching map objects through the wrapper.
For the shared map-object lifecycle and option rules, see Map object option reconciliation. For the other overlay family, see Markers and info windows.
References: src/…/polygon.tsx• L114-151 src/…/polyline.tsx• L100-137 docs/…/polygon.md• L85-106 docs/…/polyline.md• L75-96
Prop-driven option updates
Visual and behavior props update the existing map shape instead of recreating it. The wrappers track the last applied option set and send only changed options. This avoids needless redraw work, which matters most for editable shapes with many vertices.
The initial option set is applied when the shape is created. Later React renders reconcile the changed subset. Geometry follows the separate controlled or uncontrolled rules above, so changing a style option does not reset the shape's position or path.
References: src/…/set-map-object-options.ts• L7-56 src/…/circle.tsx• L88-105 src/…/rectangle.tsx• L84-100 src/…/polygon.tsx• L126-151 src/…/polyline.tsx• L112-137
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the four wrapper prop contracts aligned with the matching Google Maps option types.
- Preserve the precedence rules between controlled geometry, initial-only geometry, and encoded geometry.
- Keep inferred
clickable,draggable, andeditablebehavior consistent with the event bindings. - Keep geometry-change callbacks free from notifications caused only by the wrapper applying controlled or encoded values.
- Keep option reconciliation separate from geometry updates so a style change does not recreate or reset a shape.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A geometric wrapper renders without a Map ancestor | No map shape is attached and the wrapper reports the missing map context | Render these wrappers inside Map |
| A controlled shape is editable or draggable but its geometry callback does not update state | The next controlled render can restore the old geometry | Use onCenterChanged and onRadiusChanged, onBoundsChanged, onPathsChanged, or onPathChanged to write current geometry back to state |
clickable, draggable, or editable is explicitly set to false while its handler is present | The handler does not make that interaction available | Set the option to true when the interaction is required |
| Encoded geometry is used before the geometry library is available | The encoded value cannot be decoded and applied at that time | Let the wrapper load the geometry library and wait for it to become available |
Key files
| File | Why to read it |
|---|---|
| src/…/circle.tsx | Shows center and radius state, events, and option inference for circular overlays. |
| src/…/rectangle.tsx | Shows bounds state and bounds-change reporting. |
| src/…/polygon.tsx | Shows nested paths, holes, encoded paths, external instances, and vertex edits. |
| src/…/polyline.tsx | Shows line paths, encoded paths, external instances, and vertex edits. |
| src/…/use-maps-event-listener.ts | Defines the shared contract for attaching and removing map event listeners. |
| src/…/set-map-object-options.ts | Defines how prop changes reach an existing map shape without repeated option writes. |
| docs/…/circle.md | Gives the public circle props and controlled usage model. |
| docs/…/polygon.md | Gives the public polygon path and encoded geometry model. |