A map screen talks to three services: one that serves the tiles, one that finds a route between two points, and one that turns an address into a coordinate and back. The keyless defaults described in Maps are public community servers. They’re fine for development, but each one has a usage policy that a shipping app soon outgrows. The backend can answer all three itself, from OpenStreetMap files on its own disk, with no external map server:

  • MapTiles serves vector or raster tiles out of a single PMTiles archive.

  • OsmRouting answers OSRM’s route API, so the client’s OsrmRouteService works against it unchanged.

  • OsmGeocoding answers Nominatim’s search and reverse API, so the client’s NominatimGeocoder works against it unchanged.

All three run in the translated server and on the JVM alike, and each one is an ordinary HttpServer.Handler.

Getting the data

Both files come from OpenStreetMap. Download only the region your app covers: a city or a country is quick to load, while the whole planet needs a large server.

Tiles. The pmtiles command line tool cuts a region out of the daily Protomaps planet build without downloading the whole planet:

pmtiles extract https://build.protomaps.com/20261120.pmtiles monaco.pmtiles \
    --bbox=7.40,43.72,7.44,43.76 --maxzoom=15

Any PMTiles version 3 archive works, for example one you build yourself with Planetiler or tippecanoe. That includes raster archives of PNG, JPEG or WebP tiles.

Routing and geocoding. Both read one OpenStreetMap .osm.pbf extract. Regional extracts are published by Geofabrik. To cut a smaller area, or to re-encode a file that uses a compression other than zlib, use osmium:

osmium extract --bbox=7.40,43.72,7.44,43.76 europe-latest.osm.pbf -o monaco.osm.pbf
osmium cat some-lz4-extract.osm.pbf -o zlib.osm.pbf

The extract is read once, before the server starts, and held in memory. Load it before start(): loading is a long computation, and both files are trusted input from the operator, not something to accept from users. A country-sized extract loads in seconds and takes memory in proportion to its road network. Planet-scale routing belongs on a dedicated OSRM or Valhalla server.

Wiring the handlers

Add the handlers when you build the server. They’re tried in the order you add them, so put them before any catch-all:

OsmExtract town = OsmExtract.load("/data/monaco.osm.pbf");
Backend.builder()
        .handler(new MapTiles("/data/monaco.pmtiles", "/tiles"))
        .handler(new OsmRouting(town, "/osrm"))
        .handler(new OsmGeocoding(town, "/geo"))
        .start();

This server now answers:

  • /tiles/{z}/{x}/{y}.mvt (the extension follows the archive’s tile type) and /tiles/tiles.json, a TileJSON document that names the tile URL, the zoom range, the bounds and the attribution. A tile the archive doesn’t hold is answered with 204, and one beyond its zoom range with 404. Responses carry an ETag, and gzipped tiles are passed through to a client that accepts gzip. The TileJSON’s tile URL is built from the request’s Host header and scheme. Behind a proxy that terminates TLS that would be an http:// URL, which an HTTPS page refuses to load, so set publicUrl("https://maps.example.com/tiles") there. An archive with no attribution of its own is credited to OpenStreetMap contributors. cacheControl(…​) overrides the default caching header.

  • /osrm/route/v1/{driving|walking|cycling}/{lon},{lat};{lon},{lat}…​, with OSRM’s steps, overview and geometries (polyline or polyline6) options. Up to 25 waypoints are accepted. Each profile follows the road rules that apply to it: one-way streets and car-free roads for driving, footways and pedestrian streets for walking, cycleways for cycling.

  • /geo/search?q=…​&limit=…​ and /geo/reverse?lat=…​&lon=…​. A search ignores case and accents and matches word prefixes over place names, points of interest, streets and addresses. Towns and cities rank first.

Replace the archive file to update the tiles. The next request notices the change and reopens the archive, with no restart. A new extract for routing takes a restart.

Pointing the app at it

On the client, point each service at your server:

String server = "https://maps.example.com";
MapView map = new MapView(new MvtTileSource(server + "/tiles/tiles.json", 0, 15));
Routing.setService(new OsrmRouteService(server + "/osrm"));
NominatimGeocoder geocoder = new NominatimGeocoder(server + "/geo");
geocoder.search("casino", 5, new GeocodeCallback() {
    public void placesFound(List places) {
        // show the results
    }
    public void geocodeFailed(String message, Throwable error) {
        // tell the user
    }
});

A URL without a {z} token is read as TileJSON, so the zoom range and the tile template come from the server.

Attribution

OpenStreetMap data is licensed under the Open Database License (ODbL). Serving it from your own server doesn’t change that: every map built from it must credit OpenStreetMap. The OSMF attribution guidelines ask for:

  • The text "© OpenStreetMap contributors" where people see the map, by convention in the lower-right corner, legible and not hidden behind a control.

  • A link from that text, or close to it, to https://www.openstreetmap.org/copyright, which explains the license.

  • The same credit wherever you show results derived from the data without a map, such as a route description or a geocoding result.

The TileJSON document carries the archive’s own attribution, and every geocoding result carries the ODbL notice in its licence field. Tile styles built on other sources, such as Protomaps' basemap or Natural Earth, add their own credits; the archive’s metadata names them.

The public OpenStreetMap services have usage policies of their own: tiles and Nominatim (at most one request per second, an identifying User-Agent, no bulk geocoding). Serving the data yourself, as this chapter describes, is what those policies recommend for an app with real traffic.