Static Maps URL assembly
The Static Maps URL builder turns map options into a request URL for the Google Static Maps API. It combines the image request, viewport hints, markers, paths, visible locations, and map styles, then lets a StaticMap image or a server-side signing flow use the result.
Request model
The request has a small common envelope and several optional visual layers. The builder keeps repeated layers as repeated URL parameters so the final URL preserves the map model.
Request envelope
The request always identifies the API key and image size. Optional image and map settings add query parameters only when the caller supplies them.
The main settings are:
widthandheightdefine the requested image size.scalechanges the pixel density without changing map coverage.formatselects the image format.mapTypeselects the base map view.language,region, andmapIdcontrol label, region, and map identity choices.
The builder creates the base Static Maps endpoint and uses URL query encoding for each value. This keeps spaces, commas, pipes, and other characters safe in the final URL.
References: src/…/index.ts• L8-12 src/…/index.ts• L77-119 src/…/types.ts• L21-37
Viewport inputs
center and zoom set the map view directly. They are not needed when markers, paths, or visible locations give the Static Maps API enough information to determine the view.
visible contributes locations to the view without drawing a marker or line. The builder sends all visible locations in one pipe-separated visible value. This makes viewport control separate from map decoration.
References: src/…/index.ts• L101-112 src/…/index.ts• L131-137 src/…/types.ts• L25-35
Feature parameter encoding
Markers and paths are structured inputs, but the Static Maps API receives each one as a compact pipe-separated value. The builder formats locations first, adds feature properties, and then URL-encodes the complete value.
Location values
Every location accepts either a latitude/longitude object or a string such as an address. Object locations become lat,lng; string locations pass through as supplied.
This shared form lets markers, paths, and viewport hints use the same input model. The URL layer performs the final query encoding, so callers do not need to build their own comma or pipe escapes.
References: src/…/types.ts• L1 src/…/helpers.ts• L17-21 src/…/index.ts• L131-136
Marker definitions
Each marker value contains marker properties followed by one or more locations. Standard marker properties describe color, label, and size. Custom icon properties describe the icon URL, anchor, and scale.
Markers with the same style are combined into one markers parameter. Markers with different styles become separate parameters. This reduces repeated style data while keeping each marker location in the group that controls its appearance.
References: src/…/types.ts• L3-11 src/…/assemble-marker-params.ts• L28-78 src/…/index.ts• L121-124
Path definitions
Each path value contains optional line or fill settings followed by its coordinates. Supported path settings include color, weight, fill color, and geodesic behavior.
Coordinates can be a list of location values or a string form such as an encoded polyline. Each path remains a separate path parameter, even when two paths use the same style, so the API can draw them as separate shapes.
References: src/…/types.ts• L13-19 src/…/assemble-path-params.ts• L4-5 src/…/assemble-path-params.ts• L29-37 src/…/index.ts• L126-129
Map appearance
The URL can control both the base map and the appearance of selected map features. Base map choices use direct request settings, while detailed styling becomes one or more style parameters.
Styled map types
The style input uses Google Maps style objects. Each object selects a feature and an element, then applies its stylers. The builder converts these rules to the Static Maps form of feature, element, and styler pairs separated by pipes.
Hex color values written with # are changed to the 0x form expected by the Static Maps API. Each style object produces its own style parameter, which lets the API apply several independent rules.
References: src/…/assemble-map-type-styles.ts• L26-52 src/…/index.ts• L139-142 src/…/types.ts• L29-37
Base map choices
mapType selects the broad map view, while mapId selects a cloud-based map identity when one is needed. language and region influence how the returned map presents labels and geographic boundaries.
These settings describe the base map. Markers, paths, visible locations, and style rules then add or adjust information on that base.
References: src/…/index.ts• L101-112 src/…/types.ts• L28-37
Assembly flow
The builder assembles the request in a fixed sequence: it creates common query values, adds repeated feature values, and returns one complete URL. URL encoding happens when values are appended to the URL rather than in the feature builders.
This diagram shows how caller options become a Static Maps request that can be rendered directly or passed to the server-safe signing flow described in server-safe-integration.
flowchart LR
A[Map options] --> B[Common request parameters]
A --> C[Format locations]
C --> D[Marker parameters]
C --> E[Path parameters]
A --> F[Style parameters]
B --> G[Static Maps URL]
D --> G
E --> G
F --> G
G --> H[Render or sign]
References: src/…/index.ts• L77-144 src/…/assemble-marker-params.ts• L28-78 src/…/assemble-path-params.ts• L65-75 src/…/assemble-map-type-styles.ts• L26-52
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public option names aligned with the query names expected by the Static Maps API, especially
mapTypetomaptypeandmapIdtomap_id. - Keep marker grouping rules aligned with the marker style fields. A new marker style field can require both grouping and serialization changes.
- Keep path coordinate handling compatible with both location lists and encoded coordinate strings.
- Keep style color conversion aligned with the Static Maps style syntax.
- Keep URL encoding at the final query boundary so feature separators remain meaningful before encoding.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Missing API key | The builder warns but still returns a URL with an empty or invalid key value. | Supply the key before sending or signing the request. |
| Missing width or height | The builder warns but still creates a request with an invalid image size. | Treat dimensions as required input. |
| No center or zoom and no viewport-defining feature | The Static Maps API cannot determine the requested view. | Provide center and zoom, or provide markers, paths, or visible locations. |
| A marker style field is changed without matching grouping logic | Markers can be grouped under the wrong style or lose the new style in the URL. | Update the marker grouping and serialization contract together. |
A style color keeps # in the encoded value | The Static Maps API may reject or misread the style rule. | Use the supported 0x color form. |
Key files
| File | Why to read it |
|---|---|
| src/…/index.ts | Defines the full request assembly flow and the final URL boundary. |
| src/…/types.ts | Defines the public input model for locations, markers, paths, and request settings. |
| src/…/assemble-marker-params.ts | Shows how marker styles and locations become repeated marker values. |
| src/…/assemble-path-params.ts | Shows how path styles and coordinate forms become path values. |
| src/…/assemble-map-type-styles.ts | Shows how map style objects become Static Maps style values. |
| src/…/helpers.ts | Defines the shared location format used by request features. |
| src/…/create-static-maps-url.test.ts | Captures the URL contracts for common settings, markers, paths, and styles. |