Guides

How to add a map to your site — 12 lines, no API key, no Google

Illustration: a map spread across a table with a single pin standing in it

Almost everyone assumes a map on a site means Google Maps, which means an account, an API key and a credit card on file. Well: it doesn't. There's a combination that needs none of that — the Leaflet library plus OpenStreetMap tiles. Twelve lines, zero sign-ups, works in a plain HTML file. Let's build it.

What we're building

A chunk of page with a real interactive map: you can drag it, zoom it, and there's a marker with a caption on it. All you need is a text editor and a browser. No server, no build step.

Here's how it works. Leaflet is a small library that draws the map and handles the dragging. OpenStreetMap is an open map of the world that supplies the image squares (called tiles). Leaflet is free under the MIT licence; OSM tiles are open data.

1. Add Leaflet

In the head of your page, add the stylesheet and the script. Note the version number — it's not decoration, we'll come back to it in the gotchas.

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>

That's a CDN — the files arrive from someone else's fast server, nothing to download.

2. Make a container and give it a height

In the body, an empty block for the map to land in:

<div id="map"></div>

And a height in CSS, without fail:

#map { height: 400px; }

This is the step nine people out of ten trip on. With no height the block collapses to zero pixels, the map renders — and you see empty space. No console error will appear either.

3. Create the map and pick a centre

Now the JavaScript. The two numbers in brackets are the latitude and longitude of the centre; the third is the starting zoom (higher is closer; 12 is roughly a whole city):

const map = L.map('map').setView([51.5074, -0.1278], 12);

Getting your own coordinates takes ten seconds: open openstreetmap.org, right-click the spot you want and choose "show address" — the numbers will be in the address bar.

4. Add tiles and attribution

On its own the map is empty — it's only a canvas. The imagery arrives as a separate layer:

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
  maxZoom: 19,
  attribution: '&copy; OpenStreetMap contributors'
}).addTo(map);

The attribution line is not an option, it's a condition of use: volunteers make this map, and crediting them is required. AI-generated code drops it fairly often — put it back.

5. Drop a marker

L.marker([51.5074, -0.1278])
  .addTo(map)
  .bindPopup('We are here')
  .openPopup();

Done. Open the file in a browser — the map drags, the marker sits there, and clicking it pops up the caption.

The finished file and three gotchas

The whole thing:

<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<style>#map { height: 400px; }</style>

<div id="map"></div>

<script>
  const map = L.map('map').setView([51.5074, -0.1278], 12);
  L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
    maxZoom: 19,
    attribution: '&copy; OpenStreetMap contributors'
  }).addTo(map);
  L.marker([51.5074, -0.1278]).addTo(map).bindPopup('We are here').openPopup();
</script>

Now, what will break:

  • Coordinate order. Leaflet expects latitude first, then longitude. The GeoJSON format and some mapping services do the opposite — longitude first. Swap them by accident and your marker sails into the ocean. If a point lands somewhere odd, try flipping the numbers first.
  • A map inside a hidden block. If the container starts hidden (a tab, a modal, an accordion), the map renders at zero size and stays a grey strip. Fix it by calling map.invalidateSize() once the block is visible.
  • A version-less link. A URL like unpkg.com/leaflet without @1.9.4 will one day serve the next major version with different syntax, and your code silently stops working. Pin the version. At the time of writing, the stable release is 1.9.4.

How to place several markers at once

There's rarely just one point: branches, pickup spots, places around a city. Keep the list separate from the drawing code and loop over it — adding a new point then costs one line:

const points = [
  { coords: [51.5074, -0.1278], title: 'Head office' },
  { coords: [51.5155, -0.0922], title: 'Pickup point' },
];

points.forEach((p) => {
  L.marker(p.coords).addTo(map).bindPopup(p.title);
});

If there are many points spread out, one more trick helps: instead of a fixed centre, let the map pick the zoom so everything fits — map.fitBounds(points.map((p) => p.coords)).

You can drop this straight onto a landing page as the "find us" block.

Is it really free?

The library, entirely. OpenStreetMap tiles for a small site or side project, yes — but their servers have a usage policy: they aren't built for serious traffic and bulk downloading is banned. If the project grows, you buy tiles from a dedicated provider, and the code changes by exactly one line — the one with the URL.

How is this different from Google Maps?

In features, quite a lot (traffic, street view, its own routing). In barrier to entry, almost everything: Google wants an account, a key and billing details from the start, and quotas count every map load. For "show where we are", that's a cannon aimed at a sparrow.

What if I have an address, not coordinates?

You need geocoding — turning an address into a pair of numbers. OpenStreetMap offers a free service called Nominatim with a strict rate limit. For a single fixed point it's simpler to look the coordinates up once by hand and paste them into the code.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →