MapSnippets logo MapSnippets logo

Initialize a Map

Initialize and embed a map with tiles, pan, zoom, and markers. Each library takes its own approach, compared side by side below.

Quick recommendations

Leaflet: Best for simple embeds where minimal bundle size and fast setup matter more than visual richness.

MapLibre GL JS: Best when you need WebGL-powered vector rendering with smooth 60fps interactions and data-driven styling.

OpenLayers: Ideal for enterprise or GIS applications that need projection support, WMS/WFS, and OGC standards compliance.

MapTiler SDK: The fastest setup overall, since built-in style constants and API key config eliminate boilerplate.

At a glance

Leaflet MapLibre OpenLayers SDK
Coordinates [lat, lng] [lng, lat] fromLonLat([lng, lat]) [lng, lat]
Rendering DOM + Canvas (2D) WebGL (GPU, 60fps) Canvas + WebGL WebGL (GPU, 60fps)
Bundle size ~40 KB gzipped ~300 KB gzipped ~90 KB gzipped (tree-shakeable) ~360 KB gzipped (includes MapLibre)
Learning curve Easiest (simple API, great tutorials) Moderate (source/layer model, expressions) Steepest (projections, OGC standards, verbose API) Easy (MapLibre API with convenience helpers)
3D support No native 3D (2D only) Full: terrain, fill-extrusion, globe, sky Limited: WebGL layers, no native globe Full (same as MapLibre)
Code lines 6 11 5 11

Implementation details

Leaflet MapLibre OpenLayers SDK
Coordinate Order [lat, lng] (opposite of GeoJSON) [lng, lat] (same as GeoJSON) ol.proj.fromLonLat([lng, lat]) [lng, lat] (same as GeoJSON)
Rendering DOM + Canvas (2D raster) WebGL (GPU, 60fps vector) Canvas + WebGL WebGL via MapLibre (GPU)
Tile Format Raster PNG tiles (256 or 512px) Vector tiles (style.json) Both raster and vector (via ol-mapbox-style) Vector tiles (built-in style constants)

Code snippets

Click a library to expand its code. Each snippet shows only the core logic: the scaffold handles HTML boilerplate.

Leaflet 6 lines Show code
Full example
const map = L.map('map').setView([48.8566, 2.3522], 13);
L.tileLayer('https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', {
  tileSize: 256,
  attribution: '&copy; <a href="https://www.maptiler.com/">MapTiler</a> &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap contributors</a>'
}).addTo(map);
L.marker([48.8566, 2.3522]).addTo(map).bindPopup('Paris').openPopup();
MapLibre GL JS 11 lines Show code
Full example
const map = new maplibregl.Map({
  container: 'map',
  style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
  center: [2.3522, 48.8566],
  zoom: 13
});
map.addControl(new maplibregl.NavigationControl());
new maplibregl.Marker({ color: '#2196F3' })
  .setLngLat([2.3522, 48.8566])
  .setPopup(new maplibregl.Popup().setHTML('<b>Paris</b>'))
  .addTo(map);
OpenLayers 5 lines Show code
Full example
olms.apply('map', 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY')
  .then(function(map) {
    map.getView().setCenter(ol.proj.fromLonLat([2.3522, 48.8566]));
    map.getView().setZoom(13);
  });
MapTiler SDK 11 lines Show code
Full example
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [2.3522, 48.8566],
  zoom: 13
});
new maptilersdk.Marker({ color: '#9C27B0' })
  .setLngLat([2.3522, 48.8566])
  .setPopup(new maptilersdk.Popup().setHTML('<b>Paris</b>'))
  .addTo(map);

Things to know

Implementation-specific details that matter for initialize a map.

Leaflet

Uses raster PNG tiles, which look pixelated on high-DPI screens unless you use 512px tiles; coordinate order is [lat, lng], opposite of GeoJSON.

MapLibre GL JS

Requires a style.json URL rather than a tile URL template; heavier bundle (~300 KB) but enables vector styling and 3D features.

OpenLayers

Uses the ol-mapbox-style bridge for MapTiler vector styles; verbose API with separate View, Layer, and Map objects.

MapTiler SDK

Wraps MapLibre with MapTiler-specific conveniences like MapStyle constants and automatic API key injection; behavior is otherwise identical to MapLibre.

Which library to use?

Build maps with AI

Generate code like these examples using mapping library skills in your IDE.