Static Maps rendering and URL generation
Static Maps support produces a map image from a URL instead of creating an interactive map in the browser. The StaticMap component renders that image, while createStaticMapsUrl builds the request from map, marker, path, visibility, and style inputs. This split lets an application create the request in a server environment and render the result in a React view.
Static Maps system boundary
Static Maps use the Google Static Maps service as the image producer. The library supplies the request URL and a small React display boundary; Google returns the final map image.
flowchart LR
Inputs[Map inputs] --> URL[Static Maps URL]
URL --> Google[Google Static Maps service]
Google --> Image[Map image]
Image --> Component[StaticMap component]
The URL builder accepts a map view and optional map content. The component does not manage map state, user interaction, or a Google Maps JavaScript map instance.
StaticMap image boundary
StaticMap is a React image boundary. It takes a complete url and an optional className, then displays the image at full container width. A missing URL is treated as a configuration error.
References: src/…/static-map.tsx• L6-20
Static Maps URL generation
createStaticMapsUrl turns typed map inputs into a Google Static Maps request. The request always identifies the API key and image size, then adds only the map features that the caller provides.
Map request inputs
The request model separates the map view from the content drawn on it. A caller can provide:
- A
centeras latitude and longitude or as an address. - A
zoomlevel when the view needs an explicit scale. widthandheightfor the image size.scale,format,mapType,language,region, ormapIdfor output and map behavior.markers,paths, andstylefor visible map content.visiblelocations that affect the view without adding map markers.
The API key, width, and height are required inputs. A center and zoom are normally needed for a plain map, but the Static Maps service can derive the view from markers, paths, or visible locations.
References: src/…/types.ts• L1-37 src/…/index.ts• L77-112
Content and style inputs
Markers represent points and can use standard or custom icon properties. Paths represent lines or filled shapes and can use coordinate lists or an encoded path. Map styles change how selected map features appear.
The URL builder passes these groups to dedicated assembly logic. It keeps repeated marker styles together while preserving separate path and style entries, so the final request matches the structure expected by the Static Maps service. See Static Maps URL assembly for the detailed encoding rules.
References: src/…/assemble-marker-params.ts• L28-79 src/…/assemble-path-params.ts• L65-76 src/…/assemble-map-type-styles.ts• L26-52
Request lifecycle
The URL generation flow combines caller input with encoded request parts before the image component consumes the result.
sequenceDiagram
participant App as Application
participant Builder as URL builder
participant Maps as Static Maps service
participant View as StaticMap
App->>Builder: Supply map and content inputs
Builder-->>App: Return request URL
App->>Maps: Request image with URL
Maps-->>View: Return map image
The application may use the returned URL directly or sign it on a trusted server before it reaches the image component. The component only receives the final URL.
References: src/…/index.ts• L101-144 src/…/static-map.tsx• L14-20 docs/…/static-map.md• L19-39
Server-safe URL use
URL creation is separate from browser rendering, so the request can be prepared in server code. This is useful when an application must protect a URL-signing secret or create image URLs during server rendering.
URL signing boundary
The URL builder creates an unsigned request. If signing is required, the application signs that URL on the server and keeps the signing secret out of browser code and client requests. See Server-safe integration for the package boundary used by non-browser environments.
References: src/…/index.ts• L8-31 docs/…/static-map.md• L134-163
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public option types aligned with the parameter names expected by the Static Maps service.
- Preserve the boundary between unsigned URL creation and server-only URL signing.
- Keep marker, path, and style inputs compatible with the assembly rules described in Static Maps URL assembly.
- Keep the
StaticMapinput contract aligned with callers that generate URLs outside the browser.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing API key, width, or height | The builder warns and can return a request that the Static Maps service cannot use correctly. | Validate the required inputs before building the URL. |
| No center and zoom, and no markers, paths, or visible locations | The request has no reliable map view. | Provide an explicit view or content from which the service can derive one. |
| A signing secret reaches browser code | An attacker can create signed requests under the project identity. | Sign only in trusted server code. |
Empty url passed to StaticMap | The component throws instead of rendering an image. | Build and validate the URL before rendering the component. |
Key files
| File | Why to read it |
|---|---|
| src/…/static-map.tsx | Defines the image rendering boundary and public Static Maps exports. |
| src/…/index.ts | Shows how the full Static Maps request is composed. |
| src/…/types.ts | Defines the input model for map, marker, path, visibility, and style data. |
| src/…/assemble-marker-params.ts | Explains how marker content is grouped into request entries. |
| src/…/assemble-path-params.ts | Explains how path styles and coordinates become request entries. |
| src/…/assemble-map-type-styles.ts | Explains how map style objects become Static Maps style entries. |
| src/…/create-static-maps-url.test.ts | Shows the supported request features and the URL contracts they protect. |