How to switch map projection programmatically

This example shows how to switch between “mercator” and “globe” map projection programmatically.

The choice between Mercator and Globe can be done at different levels and moments in the lifecycle of the map, yet, unless stated otherwise, Mercator remains the default.

NPM module setup


npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';

config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
  container: 'map', // container's id or the HTML element to render the map
  style:  MapStyle.SATELLITE,
  center: [-70.8, 0], // starting position [lng, lat]
  zoom: 1, // starting zoom
  projection: 'globe' //enable globe projection
});

document.getElementById('projection').addEventListener('click', function () {
  const projection = map.getProjection()
  if (projection.type === 'globe') {
    map.setProjection("mercator", { persist: true });
  } else {
    map.setProjection("globe", { persist: true });
  }
});
Projection control how to toggle the map between mercator and globe projection

Projection control (globe)

Tutorials

Toggle between Mercator and globe projections using map controls.

How to turn on the globe projection

Globe projection

Examples

Enable the 3D globe map projection in your application.

Add a 3D model to globe using MapTiler 3D JS

Add a 3D model to globe using MapTiler 3D JS

Examples

Integrate 3D models into your interactive globe map surface.

Create a globe map with 3D terrain elevation

Globe with elevation terrain

Examples

Create a 3D globe map featuring terrain elevation.

Was this helpful?