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 / geometric-overlays

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.

WrapperGeometryMain controlled valueInitial-only value
CircleA center point with a radius in meterscenter, radiusdefaultCenter, defaultRadius
RectangleNorth, south, east, and west boundsboundsdefaultBounds
PolygonOne path or several paths, including holespathsdefaultPaths
PolylineOne ordered line pathpathdefaultPath

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.

  • fillColor and fillOpacity apply to filled shapes.
  • strokeColor, strokeOpacity, and strokeWeight control outlines.
  • icons applies to polylines.
  • geodesic changes 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:

HandlerInferred optionShapes
onClickclickable: trueAll
onDrag, onDragStart, onDragEnd, or geometry-change handlerdraggable: trueAll
Geometry-change handlereditable: trueAll

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:

  • onClick runs when the shape is clicked.
  • onMouseOver and onMouseOut run when the pointer enters or leaves the shape.
  • onDragStart, onDrag, and onDragEnd report 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, and editable behavior 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

TriggerConsequenceGuard
A geometric wrapper renders without a Map ancestorNo map shape is attached and the wrapper reports the missing map contextRender these wrappers inside Map
A controlled shape is editable or draggable but its geometry callback does not update stateThe next controlled render can restore the old geometryUse 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 presentThe handler does not make that interaction availableSet the option to true when the interaction is required
Encoded geometry is used before the geometry library is availableThe encoded value cannot be decoded and applied at that timeLet the wrapper load the geometry library and wait for it to become available

Key files

FileWhy to read it
src/…/circle.tsxShows center and radius state, events, and option inference for circular overlays.
src/…/rectangle.tsxShows bounds state and bounds-change reporting.
src/…/polygon.tsxShows nested paths, holes, encoded paths, external instances, and vertex edits.
src/…/polyline.tsxShows line paths, encoded paths, external instances, and vertex edits.
src/…/use-maps-event-listener.tsDefines the shared contract for attaching and removing map event listeners.
src/…/set-map-object-options.tsDefines how prop changes reach an existing map shape without repeated option writes.
docs/…/circle.mdGives the public circle props and controlled usage model.
docs/…/polygon.mdGives the public polygon path and encoded geometry model.