MapSnippets logo MapSnippets logo

Initialize a MapLibre GL JS Map

MapLibre GL JS

Initialize a MapLibre GL JS map with maplibregl.Map and the MapTiler streets-v4 style, then add a NavigationControl and a Marker with a Popup.

progress_activity Loading map...
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);

How to create a MapLibre GL JS map with a MapTiler style

To initialize a map, call the maplibregl.Map constructor with a container element, a style URL (which defines sources, layers, fonts, and sprites), an initial center, and a zoom. MapLibre GL JS renders vector tiles using WebGL for smooth 60fps performance.

MapLibre uses [lng, lat] coordinate order, the same as GeoJSON but the opposite of Leaflet.

The style URL points to a MapTiler Cloud style.json that configures everything MapLibre needs. NavigationControl adds zoom and rotation buttons. Markers are DOM elements positioned on the map.

Key APIs

API Description Docs
maplibregl.Map Create map instance maplibre.org/maplibre-gl-js/docs/API/classes/Map
maplibregl.NavigationControl Zoom/rotate buttons maplibre.org docs
maplibregl.Marker Add DOM marker maplibre.org docs

FAQ

What is a style.json?

A JSON document that tells MapLibre where to load tiles, how to render layers, and which fonts and icons to use. MapTiler provides 16+ ready-made styles.

How is MapLibre different from Mapbox GL JS?

MapLibre is the open-source fork of Mapbox GL JS v1. It keeps the v1 API, has no proprietary restrictions, and is developed by an active community.

Common mistakes

  • Using [lat, lng] instead of [lng, lat]. MapLibre uses the GeoJSON coordinate order
  • Adding layers before the map loads instead of inside map.on('load', () => { ... })
  • Forgetting the container height. The map needs explicit dimensions

Build maps using MapLibre GL JS with AI

Generate code like this initialize a maplibre gl js map example using the MapLibre GL JS skill in your IDE.

Related examples