How to control what StaticMaps.render/1 draws: the options, how the view is fitted, markers, the attribution strip, and what happens when a tile cannot be fetched. For where the tiles come from, see Tile sources and caching.

Options

{:ok, png} =
  StaticMaps.render(
    center: {14.5547, 121.0244},
    zoom: 15,
    size: {800, 400},
    markers: [{14.5547, 121.0244}]
  )

Coordinates are always {latitude, longitude}. Several libraries in this space take [lng, lat], so check the order when porting code.

OptionDefaultMeaning
:centerfitted{lat, lng}
:zoomfittedinteger, within the source's zoom range
:bounds—{{south, west}, {north, east}}; the view is fitted to it
:padding20pixels kept clear around :bounds and markers when fitting
:size{600, 400}output pixels, before :scale; at most {2048, 2048}
:scale12 renders at twice the pixel density for retina
:markers[]{lat, lng} or {lat, lng, opts}; see Markers
:sourceStaticMaps.Source.OSMa module, or {module, opts}, implementing the behaviour; or a list of them, tried in order
:format:png:png or :jpeg
:background"#e0e0e0"colour for anything no tile covers, such as beyond the poles
:attributiontruesee Attribution
:on_tile_error:fail:fail or :fill; see When a tile fails

render/1 returns {:ok, binary} or {:error, exception}. It never raises for bad input. An unknown option, a zoom outside the source's range or a size over the limit is an {:error, %ArgumentError{}}.

It returns bytes and nothing else: it does not write files or upload anywhere, and it takes plain {lat, lng} tuples rather than anyone's geo struct. Where the image lives, and how your own location data becomes tuples, stays in your application.

Fitting the view

You do not have to pick a centre and zoom yourself.

  • Give :center and :zoom, and you get exactly that view.
  • Give :bounds, and StaticMaps picks the highest zoom at which the bounds, the markers and :padding all fit, and centres on them.
  • Give only :markers, and it fits to the markers. A single marker, or several at the same spot, gets zoom 15, or the nearest zoom the source serves.
  • Give :center without :zoom, and it keeps that centre and picks the highest zoom at which every marker is still in view; for a single marker, no higher than zoom 15.
  • Give :zoom without :center, and it keeps that zoom and centres on the bounds and markers.

The zoom is chosen from the zooms the first source serves. When nothing fits even at its lowest zoom, the lowest zoom is used.

Fitting counts each marker's icon size, not just its point, so a pin at the edge is never half cut off. It also leaves room for the attribution strip.

A :bounds that crosses the antimeridian is written with west > east, for example {{-20, 170}, {-10, -170}} for a box around Fiji. Markers need no such care: they are fitted by the shortest span of longitude that covers them all, so markers either side of the antimeridian frame the Pacific, not the whole world.

Latitudes are clamped to ±85.0511°, the limit of the Web Mercator projection. Anything beyond that, and anything past the poles at low zoom, is painted :background rather than filled with the wrong tiles.

Zoom is always an integer. Tiles only exist at integer zooms, and scaling them to fit a fractional zoom blurs every label on the map.

Markers

Each marker is a default pin, 24 × 36 pixels with its tip on the coordinate, unless its options say otherwise:

markers: [
  {14.5547, 121.0244},
  {14.5176, 121.0509, color: "#1565c0", label: "B"},
  {14.5995, 120.9842, icon: File.read!("priv/office.svg"), anchor: {16, 32}}
]
OptionDefaultMeaning
:color"#d63e2a"the pin's colour: "#rgb", "#rrggbb" or {r, g, b}
:labelnoneup to three characters written in the pin's head, in black or white, whichever reads better on the colour
:iconthe pinSVG or image bytes to draw instead of the pin
:anchorbottom centre{x, y} in the icon's pixels that sits on the coordinate

Pins and SVG icons are drawn at the output scale, so they stay sharp at scale: 2. An image icon is enlarged for scale: 2, so give an SVG where you can. A label is drawn only on the default pin.

Markers are drawn in the order given, later ones on top, and at low zoom a marker appears on every copy of the world the image shows.

Attribution

Tiles carry licence terms. OSM's standard tiles are CC BY-SA 2.0 and the data is ODbL, both of which require credit, and every commercial provider requires their own.

render/1 therefore draws the source's attribution into the corner of the image by default: dark text on a translucent white strip, drawn last so nothing covers it. On a narrow image the text wraps rather than being cut off. When a fallback source rendered the image, its credit is the one drawn.

You can move it with attribution: [position: :bottom_left], and fitting keeps markers clear of it wherever it is. You can render it yourself in surrounding markup, but turning it off in the image takes attribution: false, which is a deliberate act and is your licence violation to explain, not the library's.

:attribution is an option in your code only. StaticMaps.Plug never reads it from a request.

When a tile fails

A render needs every tile it covers. When one cannot be fetched after retries, the default is on_tile_error: :fail, and render/1 returns {:error, %StaticMaps.TileError{}} naming the tiles that failed.

on_tile_error: :fill paints the missing tiles with :background and returns the image anyway. Use it when a map with a grey square is better than no map, and do not cache the result for long.

A map with a hole in it that looks like a success is the worst outcome, because it gets cached and served for weeks. That is why :fail is the default.

A source that fails several renders in a row because it is down, unreachable or erroring is skipped for a while, 30 seconds by default, rather than every render waiting for it to fail again. Missing tiles, such as a 404, do not count. The next source in the list is used instead, or, if there is none, render/1 returns at once with {:error, %StaticMaps.UnavailableError{}}. The same error, listing each source's failure, comes back when a render has backup sources and every one of them fails.

config :staticmaps, :circuit_breaker,
  threshold: 5,      # failed renders in a row; nil turns the breaker off
  cooldown: 30_000   # milliseconds a failing source is skipped for