StaticMaps (staticmaps v0.1.1)

Copy Markdown View Source

Renders map images on the server.

render/1 fetches the tiles that cover a view from a tile source, stitches them into one image, draws markers and the source's attribution strip over it, and returns the encoded PNG or JPEG. The view is either a centre and zoom, or fitted to bounds and markers. StaticMaps.Plug serves the same renders from signed URLs.

Summary

Functions

Renders a map and returns the encoded image.

Functions

render(opts)

@spec render(keyword()) :: {:ok, binary()} | {:error, Exception.t()}

Renders a map and returns the encoded image.

Options

  • :center — {lat, lng} in degrees. Required unless :bounds or :markers are given, in which case it is fitted when left out.
  • :zoom — a non-negative integer that the source serves. Required unless :bounds or :markers are given, in which case it is fitted when left out. A 512-pixel source serves render zooms one above its tile zooms; see StaticMaps.Source.
  • :bounds — {{south, west}, {north, east}} in degrees, to fit the view to; west > east crosses the antimeridian.
  • :markers — a list of {lat, lng} or {lat, lng, opts}, drawn in order and fitted into the view; see StaticMaps.Marker for opts. Defaults to [].
  • :padding — logical pixels kept clear around :bounds and :markers when the view is fitted; defaults to 20.
  • :source — a module implementing StaticMaps.Source, or {module, opts}, or a list of them: the first is the primary source and the rest are fallback sources, tried in order when it fails. A fallback that does not serve the zoom is skipped. Defaults to StaticMaps.Source.OSM.
  • :size — {width, height} in logical pixels, before :scale, at most {2048, 2048}; defaults to {600, 400}
  • :scale — 1 or 2; defaults to 1
  • :format — :png or :jpeg; defaults to :png
  • :background — "#rrggbb", "#rgb" or {r, g, b}, painted wherever no tile covers the image; defaults to "#e0e0e0"
  • :attribution — where the attribution strip goes: true for the bottom right, [position: :bottom_right | :bottom_left], or false to leave it off. Defaults to true. A fitted view keeps the strip's height clear.
  • :on_tile_error — what happens when every source has a tile that cannot be fetched or decoded. :fail returns the error; :fill returns the image from the first source that was tried, with its failed tiles painted :background. Defaults to :fail.

Errors

  • %ArgumentError{} for invalid options
  • %StaticMaps.TileError{} when the only source tried has tiles that fail
  • %StaticMaps.UnavailableError{} otherwise, when every source failed or was skipped
  • %RuntimeError{} when libvips fails, such as when no font is installed to draw the attribution with

Errors are returned as {:error, exception} rather than raised.