Skip to content

Draw a boundary on a web map

Show the outline of any country, state, county, city or postcode on a Leaflet or MapLibre map. The calls below need no key and no account: paste the HTML into a file and open it.

Leaflet

A boundary comes back as a GeoJSON Feature, which L.geoJSON draws as it is. r62422 is Berlin's id; find any other with a search.

index.html
<div id="map" style="height: 400px"></div>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script>
  const map = L.map("map");
  L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
    attribution: "© OpenStreetMap contributors",
  }).addTo(map);

  // Berlin, at low detail: free, no key.
  fetch("https://geocodeai.dev/v1/boundaries/r62422?detail=low")
    .then((res) => res.json())
    .then((feature) => {
      const layer = L.geoJSON(feature).addTo(map);
      map.fitBounds(layer.getBounds());
    });
</script>

MapLibre GL

Add the Feature as a GeoJSON source and style it with a fill and a line layer. Every boundary carries a bbox to fit the map to.

index.html
<div id="map" style="height: 400px"></div>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css">
<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
<script>
  const map = new maplibregl.Map({
    container: "map",
    style: "https://demotiles.maplibre.org/style.json",
  });

  map.on("load", async () => {
    const feature = await (await fetch("https://geocodeai.dev/v1/boundaries/r62422?detail=low")).json();
    map.addSource("boundary", { type: "geojson", data: feature });
    map.addLayer({ id: "fill", type: "fill", source: "boundary", paint: { "fill-color": "#0ea5e9", "fill-opacity": 0.25 } });
    map.addLayer({ id: "line", type: "line", source: "boundary", paint: { "line-color": "#0369a1", "line-width": 2 } });
    const [west, south, east, north] = feature.bbox;
    map.fitBounds([[west, south], [east, north]], { padding: 24 });
  });
</script>

From a place name

Search returns the best matches first, each with its id and kind (country, state, county, city…). Pass country or kind to narrow it; see search.

boundary.js
// Find a boundary by name, then fetch its polygon. Both calls are free without a key.
async function boundaryFor(name) {
  const search = await (await fetch("https://geocodeai.dev/v1/boundaries/search?q=" + encodeURIComponent(name))).json();
  const best = search.results[0];
  if (!best) throw new Error("No boundary called " + name);
  return (await fetch("https://geocodeai.dev/v1/boundaries/" + best.id + "?detail=low")).json();
}

const feature = await boundaryFor("Cook County");

Before you ship

  • Calls without a key are limited to 20 a minute per IP address and return detail=low only. For a public map, fetch each polygon once on your server with a free key, store it, and serve it yourself. Never put a key in browser code.
  • Need more points along the border? medium, high and full detail and the other formats are on Detail & formats.
  • Boundaries are OpenStreetMap data (ODbL): show “© OpenStreetMap contributors” wherever you draw them, as the tile layers above already do.
  • Want to know which boundary a click or a GPS point falls in? Use reverse lookup or point checks.