Where a map's tiles come from, how to add your own source, how to fall back to another when one fails, and how the tile cache keeps fetches down. For ready-made configurations of common providers, see Tile providers.
Built-in sources
StaticMaps.Source.OSM— OpenStreetMap's standard style, and the default. Fine for development and light use; read The OpenStreetMap tile servers before you point production at it.StaticMaps.Source.Template— any{z}/{x}/{y}URL template, which covers most commercial providers with a key in the URL:source: {StaticMaps.Source.Template, url: "https://tiles.example.com/{z}/{x}/{y}.png?key=…", attribution: "© Provider"}
A source for PMTiles archives, one file of tiles on your own storage with no tile server, is planned; see the roadmap.
Sources are modules in your code, never values taken from a request. A tile URL built from user input turns your server into a proxy for anyone who asks.
Writing a source
A source is a module that turns a tile {z, x, y} into image bytes, and says what its
tiles look like and what credit their licence requires. For a server with a URL per tile,
use StaticMaps.Source.HTTP and describe the URL:
defmodule MyApp.Tiles do
use StaticMaps.Source.HTTP
@impl true
def url(%{z: z, x: x, y: y, scale: scale}, _opts) do
suffix = if scale == 2, do: "@2x", else: ""
"https://tiles.example.com/#{z}/#{x}/#{y}#{suffix}.png"
end
@impl true
def info(_opts) do
%{attribution: "© Example Maps", tile_size: 256, zoom: 0..19, scales: [1, 2]}
end
endStaticMaps.Source.HTTP supplies the fetching: the User-Agent, timeouts, retries, a
limit on concurrent requests and the tile cache. Implement headers/2 to send an API key
or other headers a server needs. A source that does not use HTTP implements fetch/2
itself instead of url/2.
info/1 matters more than it looks:
:tile_sizeis 256 or 512 pixels. Getting it wrong puts every tile in the wrong place.:zoomis the range the source serves.render/1rejects a zoom outside it rather than fetching tiles that do not exist.:scaleslists the pixel densities the source has tiles for. Withscale: 2, a source that lists2provides sharp high-density tiles. One that does not has its tiles scaled up, so the map is soft but markers and attribution are still sharp.
Backup sources and mirrors
Give :source a list, and when a render fails from the first source, the whole map is
rendered again from the next:
source: [
{StaticMaps.Source.Template, url: "https://…", attribution: "© Provider"},
StaticMaps.Source.OSM
]The fallback always renders the whole map. Different providers draw different maps, so filling in one provider's missing tiles from another would give a visible patchwork, and an attribution that credits only one of them. A fallback that does not serve the requested zoom is skipped.
Servers that serve identical tiles, such as your own tile servers behind different
hosts, are mirrors of one source instead. Source.Template takes a list of URL
templates, sends each tile to a mirror chosen from its position, and fetches a tile from
the next mirror when its own fails. Since the tiles are identical, mixing mirrors within
one image is safe. Your own tile servers shows
the configuration.
Neither feature rotates between different free services to stay under each one's usage limits. That spreads heavy use across volunteers rather than reducing it.
The tile cache
Tiles are cached by {source, z, x, y, scale}, because adjacent subjects share almost
all of their tiles — a hundred properties in one city are not a hundred times the
fetches. The cache:
- keeps a tile for as long as the source's
Cache-ControlorExpiresheader allows, and for 7 days when the source sends neither, as OSM's policy requires; - revalidates an expired tile with
If-None-MatchorIf-Modified-Sinceinstead of downloading it again, and serves the expired tile if the server cannot be reached; - only stores a response that is a decodable image of the expected size, so a provider's HTML error page never becomes a tile;
- is limited in size, 64 MB in memory by default, and evicts the least recently used tiles when full.
The default cache lives in ETS and is lost on restart. Implement the
StaticMaps.TileCache behaviour to keep tiles somewhere else:
config :staticmaps, :tile_cache,
module: StaticMaps.TileCache.ETS, # nil turns caching off
max_bytes: 64 * 1024 * 1024Tiles from HTTP sources are cached. A source that reads tiles from your own storage does not implement the cache's callback, and its tiles are not copied into memory.
Sources that must not be cached
Several commercial providers forbid keeping their tiles on a server. Give such a source
cache: false, and its tiles are never stored or served from the cache, while other
sources, such as a fallback, are still cached:
source: [
{StaticMaps.Source.Template, url: "https://…", attribution: "© Provider", cache: false},
StaticMaps.Source.OSM
]Tile providers says which providers need it. Some also restrict
serving rendered images of their tiles at all, which cache: false does not address.
Rendered images
The library does not cache rendered images. You already know the identity of the thing being rendered and where its image belongs, so you are better placed to store it. If you would rather not store images at all, render them on request and let browsers and a CDN hold them; see Serving over HTTP.
The OpenStreetMap tile servers
If you use Source.OSM in production, you are using infrastructure donated by volunteers.
OSM's tile usage policy asks for
an identifying User-Agent, forbids bulk downloading, and asks heavy users to move to
their own infrastructure. StaticMaps sends a proper User-Agent, makes at most two
concurrent requests to OSM, and caches as required. It will not pre-seed a region for
you, because that is bulk downloading no matter how it is dressed up.
Caching is also not a loophole. It reduces your load on their servers; it does not turn heavy use into light use. If this library is carrying real traffic for you, a paid provider or your own tiles are the honest answer, and switching is one line.