MapSnippets logo MapSnippets logo

Globe Projection with MapLibre GL JS

MapLibre GL JS

Turn a MapLibre GL JS map into a 3D globe by calling map.setProjection() with type globe after style.load, shown on the MapTiler satellite style.

progress_activity Loading map...
const map = new maplibregl.Map({
  container: 'map',
  style: 'https://api.maptiler.com/maps/satellite/style.json?key=YOUR_MAPTILER_KEY',
  center: [0, 20],
  zoom: 1.2
});
map.addControl(new maplibregl.NavigationControl());
map.on('style.load', () => {
  map.setProjection({ type: 'globe' });
});

How to show a 3D globe in MapLibre GL JS

MapLibre GL JS supports globe projection through the setProjection() method. When using an external style URL (like MapTiler styles), call map.setProjection({ type: 'globe' }) after the style.load event fires. Waiting for this event makes sure the style is fully loaded before the projection changes.

The globe projection renders the map as a sphere at low zoom levels. As users zoom in past level 5-6, MapLibre transitions from the curved globe view to a flat Mercator projection. The satellite basemap works particularly well for globe visualization.

To add atmosphere effects, define a sky property in the style with atmosphere-blend, which creates a subtle glow at the globe edges. You can also toggle back to flat Mercator at runtime with map.setProjection({ type: 'mercator' }).

Key APIs

API Description Docs
map.setProjection() Switch between globe and mercator maplibre.org docs
style.load event Fires when external style is ready maplibre.org docs
sky style property Atmosphere/sky configuration maplibre.org style spec

FAQ

Why not set projection in the constructor?

When using an external style URL, the projection must be set after the style loads. Setting projection: 'globe' in the constructor only works with inline style objects, not URLs.

Can I use globe with 3D terrain?

Yes. Globe projection and terrain DEM work together. Add terrain as usual inside the style.load event.

Does globe projection affect performance?

Globe rendering adds minimal overhead. The transition from globe to Mercator happens automatically at higher zoom levels where the curvature becomes negligible.

Common mistakes

  • Setting projection: 'globe' in the constructor with an external style URL. It is ignored there, so call setProjection() after style.load
  • Using the load event instead of style.load. setProjection needs a parsed style, which style.load guarantees
  • Passing the string 'globe' instead of the object { type: 'globe' }, which may not work in all versions

Build maps using MapLibre GL JS with AI

Generate code like this globe projection with maplibre gl js example using the MapLibre GL JS skill in your IDE.