Home / GIS and Open Map Data for Beginners

How to Use OpenStreetMap in a Website: The No Code Embed and the Leaflet Route

A person coding on a laptop with HTML code on screen

Photo: Lukas Blazek / Pexels

A map on a website used to mean a Google API key, a billing account, and a terms page. OpenStreetMap offers the other bargain: the map is free, the standard library is tiny and open source, and the only non negotiable price is a credit line. There are two honest routes in. The no code route embeds the map from openstreetmap.org itself through an iframe, suitable for a contact page, a venue, a club's meeting place. The developer route builds the map with Leaflet, the open source JavaScript library (version 1.9.4, about 42 KB gzipped, no API key), which is the same library this site loads to power its own map tools, so the worked example below is not hypothetical. This guide walks both routes end to end, then covers the governance that keeps the free route free: the tile usage policy, attribution done right, and the scale at which you should move to a commercial tile provider or self hosting. The wider API landscape, geocoding and routing services included, is our how to use OpenStreetMap API guide; this page is the map on the page.

Interactive map

Coordinate Explorer: Pick the Center Your Embed Will Open On

Click the place your web map should open on and read its pair. That latitude and longitude pair is the setView call in the Leaflet route and the marker position in the no code embed.

Decimal degrees40.689167, -74.044444
Latitude40.689167° N
Longitude74.044444° W
Degrees minutes seconds40° 41' 21.00" N, 74° 02' 40.00" W
Degrees decimal minutes40° 41.350' N, 74° 02.667' W
Plus code87G7MXQ4+M67

Map data © OpenStreetMap contributors. Coordinates are read from the map itself, in the WGS 84 system your phone uses.

Route One: The No Code Embed

On openstreetmap.org, navigate to the exact view you want to show: pan and zoom until the framing is the framing your page needs, because the embed preserves it. If the map should carry a marker, search the place first so the marker exists, or use the share flow's marker option. Click the Share icon in the right hand toolbar. In the panel that opens, choose the HTML option: it offers a snippet of iframe code, with a checkbox to include a marker at the map's center. Copy the snippet and paste it into your page's HTML where the map belongs. That is the entire procedure. The iframe loads the map from OpenStreetMap's own servers, arrives with the attribution built into its corner, and responds to dragging and zooming like the source site. Its limits are the embed's nature: styling is the standard map style, behavior is the standard behavior, and the iframe is a window onto their site rather than a component of yours. For a "find us here" map, those limits rarely matter, and the route's zero maintenance is a genuine virtue: nothing to update, nothing to break when libraries change.

Route Two: Leaflet, the Developer Standard

Leaflet is a small library that gives you a map object and lets you layer the world onto it. Include its CSS and JavaScript from a CDN (or your own host), give it a div, and three lines do the essential work. In words, then in the pattern every Leaflet tutorial shares: create the map centered on a coordinate pair at a chosen zoom with L.map and setView; add the OpenStreetMap tile layer with L.tileLayer, pointing at the standard tile URL pattern, https://tile.openstreetmap.org/{z}/{x}/{y}.png, where z is zoom and x and y are tile coordinates the library computes for you; set maxZoom 19 and the attribution string, "© OpenStreetMap contributors"; then add what your page needs, an L.marker at your location with a popup, circles, lines, or GeoJSON layers of your own data. The pair you center on comes from the same coordinate craft as everywhere on this site: find it with the right click gesture in our coordinates on Google Maps guide, or click the explorer above, whose map is this very stack running. Leaflet's plugin ecosystem covers search, routing display, clustering, and drawing, so the route scales from a contact map to a real application without changing libraries.

A developer typing code on a laptop
A developer typing code on a laptop

Attribution: The One Non Negotiable

Both routes carry the same duty, and it is the license's visible price. OpenStreetMap data is published under the Open Database License, and the project's requirement for maps on websites is a credit to OpenStreetMap contributors, linked where practical to openstreetmap.org and to the license. The no code iframe includes it automatically. The Leaflet route includes it because you set the attribution control's text, as in the pattern above, and Leaflet displays it in the map's corner; removing or hiding that control is the one customization you must not make. This is not pedantry: the credit is how the commons advertises itself into existence, and it is the entirety of what the data costs you. The full duties, including what applies when you build databases from the data, are audited in is OpenStreetMap free and worked in how to use OpenStreetMap data.

The Tile Usage Policy, and When to Leave the Free Tiles

Here is the part tutorials skip, and it matters. The tiles at tile.openstreetmap.org are served by community run servers, donated capacity, and the project's tile usage policy governs them: they are for modest, genuine browsing use, not for bulk downloading, not for prefetching whole regions, and not as the free infrastructure of a high traffic commercial product. A small site, a blog, a club, a portfolio, embedding Leaflet with the standard layer is exactly the welcomed use. A successful app, a busy store locator, a product whose map loads millions of tiles, is not: at that scale the honest routes are a commercial tile provider (companies that serve OSM based tiles for a fee, with service guarantees) or self hosting (rendering or serving tiles from your own copy of the data, the extract workflow of our data guide, on your own servers). The signal to switch is success itself: when your map traffic becomes a line item someone could notice, it should be a line item you pay for. The policy is not a trap; it is a commons asking its heaviest users to carry infrastructure, and complying early is cheaper than being blocked mid growth.

Practical Details That Decide Whether It Feels Professional

Height and responsiveness: the map div needs an explicit height in CSS or it collapses to zero; set a height that behaves on mobile (a common pattern is a fixed pixel height on desktop and a shorter one under a width breakpoint), and let the container be full width. The marker's popup should contain the one action the visitor came for: the address, a directions link, the phone number; a bare pin is a missed errand. Do not fight the scroll: on mobile, a map that hijacks page scrolling traps visitors; Leaflet's options let you temper scroll wheel zoom on desktop, and touch behavior deserves a test on a real phone. Fallback: if tiles fail to load, the visitor should still find the address in text on the page; the map is an enhancement of the information, never its only copy. Test the pair: click your own marker and confirm it sits on your building, not the similar named street across town; coordinate order, latitude then longitude in setView, is the classic slip, and it lands a European business in the ocean off Africa with the values swapped.

Which Route Should You Take?

Take the embed if the map is a fixture: one location, standard looks, no behavior beyond pan and zoom, and nobody on your team wants to own JavaScript. Take Leaflet if the map is a feature: multiple markers, your own data as layers, styling that matches your site, popups with actions, or any interaction beyond looking. Take a provider or self hosting when the traffic outgrows the commons, on either route. And whichever you take, you have joined the arrangement that makes this ecosystem unusual: the map on your page exists because volunteers mapped the world and gave it away under one condition worth honoring in the corner of every map, their names on the work. The site you are reading runs its tools on the same library and the same data, credited the same way, which is the strongest recommendation this guide can honestly give.

Related Guides

If this page answered part of the question, these GIS and open map data guides cover the rest of the same ground:

Frequently Asked Questions

How do I put OpenStreetMap on my website without coding?

Use the embed: on openstreetmap.org, frame the view you want, click the Share icon, choose the HTML option (with the marker checkbox if you need a marker), and paste the iframe code into your page. Attribution is included in the embed automatically. No account or API key is involved.

What is Leaflet and do I need an API key?

Leaflet is the standard open source JavaScript library for web maps, about 42 KB gzipped at version 1.9.4. It needs no API key and no account. You load the library, create a map with setView on a coordinate pair, and add the OpenStreetMap tile layer with its attribution string.

What attribution does OpenStreetMap require on a website map?

A visible credit to OpenStreetMap contributors, linked to openstreetmap.org where practical, as required under the Open Database License. In Leaflet this is the attribution option on the tile layer, displayed in the map corner. Do not remove or hide it; it is the license's price for the data.

Can I use the free OpenStreetMap tiles on a commercial website?

For modest traffic, yes; the standard tiles are community servers under a usage policy that welcomes genuine browsing use. Heavy or product scale use should move to a commercial OSM tile provider or self hosted tiles from your own data copy. Bulk downloading and prefetching tiles are against the policy at any scale.

Why does my Leaflet map show gray or land in the wrong place?

Gray usually means the container has no height in CSS, or the tile layer URL or attribution setup is broken. The wrong place is usually a swapped coordinate pair: setView takes latitude first, then longitude. A pair entered in the other order lands in a mirrored location, often an ocean, which is the tell.

Photo credits: Lukas Blazek (Pexels); olia danilevich (Pexels). Photos are served from this site; follow the credit to the library page for the original.