Skip to main content

@math.gl/geospatial/web-mercator

:::caution Deprecated

@math.gl/geospatial/web-mercator is considered deprecated. Prefer @math.gl/geospatial/projection for new work.

:::

@math.gl/geospatial/web-mercator provides a map camera and coordinate utilities for spherical Web Mercator. Use WebMercatorViewport to convert longitude/latitude positions to screen pixels and back with zoom, pitch, and bearing.

Geographic inputs use [longitude, latitude] in degrees, with optional altitude in metres. For CRS conversions beyond Web Mercator, use projection.

Usageโ€‹

npm install @math.gl/geospatial
import {WebMercatorViewport} from '@math.gl/geospatial/web-mercator';

// A viewport looking at San Francisco city area
const viewport = new WebMercatorViewport({
width: 800,
height: 600,
longitude: -122.45,
latitude: 37.78,
zoom: 12,
pitch: 60,
bearing: 30
});

viewport.project([-122.45, 37.78]);
// returns pixel coordinates [400, 300]
viewport.unproject([400, 300]);
// returns map coordinates [-122.45, 37.78]

Featuresโ€‹

FeatureSupportNotes
Longitude/latitude and world-coordinate conversionโœ…Coordinate utilities define spherical Mercator, zoom scales and projection/view matrices.
EPSG:3857 metre-coordinate conversionโœ…Explicit forward/inverse helpers distinguish metre coordinates from pixel-scaled world coordinates.
Zoom, pitch and bearing camera projectionโœ…WebMercatorViewport projects/unprojects geographic and screen positions.
Bounds fitting and viewport normalizationโœ…fitBounds, getBounds and normalized camera properties support explicit viewport/padding contracts.
Distance scales and metre offsetsโœ…Latitude-dependent scale helpers and addMetersToLngLat support local approximations within documented limits.
Fly-to interpolationโœ…flyToViewport and getFlyToDuration provide camera-path interpolation helpers.
Ellipsoidal Mercator and arbitrary CRS camerasโŒThis camera model is spherical Web Mercator; use the projection module for other CRS transforms.
Terrain-depth and mesh intersection pickingโŒScreen/plane unprojection is not a terrain/depth-buffer intersection engine.
General globe/flat rendering and GPU ownershipโŒThe public viewport API does not supply a complete globe renderer, shared render-state transition, or GPU resource lifecycle.

Coordinate utilitiesโ€‹

The utility reference covers world coordinates, distance scales, zoom conversions, and EPSG:3857 coordinates in metres. World coordinates and EPSG:3857 metres use different scales; select the matching forward and inverse helpers.

The module continues the API from the archived viewport-mercator-project package.