MapMath

Map math formula reference

Every map mathematics formula from the MapMath guide, free to consult. Each formula links to the chapter that derives it and shows working JavaScript code — bookmark this page if you write geospatial code regularly.

Notation: = latitude (radians), = longitude (radians), = Earth's mean radius (6,371 km).

Distance

Haversine formula

Great-circle distance between two points on a sphere. Numerically stable across all distances; accurate to roughly 0.5% when using WGS84 mean radius. The standard choice for almost all geospatial distance calculations.

Distance on a sphere — Haversine and friends

Spherical law of cosines

Direct formulation of great-circle distance. Mathematically equivalent to Haversine but loses precision at short distances due to floating-point limits near arccos(1). Use Haversine instead for production code.

Distance on a sphere — Haversine and friends

Equirectangular approximation

Fast flat-Earth approximation. Accurate to ~0.1% over distances under 50 km; error grows quadratically beyond that. Useful for sorting/clustering nearby points where absolute accuracy doesn't matter.

Distance on a sphere — Haversine and friends

Bearing & destination

Initial bearing (forward azimuth)

Compass bearing from point 1 to point 2 at the moment of departure. Note: the bearing changes continuously along a great-circle path; use the final bearing formula at arrival.

Bearings — compass directions on a sphere

Destination point

Given a start point, a bearing θ, and an angular distance δ = d/R, compute the destination. Used for geofences, range rings, dead reckoning, and any 'move a point N km in direction θ' operation.

Destination points — moving on a sphere

Projections

Web Mercator — forward (lat/lon → x, y)

Maps spherical coordinates to a flat plane (EPSG:3857). Used by Google Maps, OpenStreetMap, Mapbox, and virtually every web map. Distorts area severely near the poles; valid latitude range is ±85.05113°.

Map projections — flattening the globe

Web Mercator — inverse (x, y → lat/lon)

The inverse projection — recover lat/lon from Mercator x, y. Essential for click-to-coordinate conversion in interactive maps.

Map projections — flattening the globe

UTM zone number

UTM divides the globe into 60 zones, each 6° of longitude wide. Within a zone, UTM gives metre-accurate flat coordinates suitable for surveying and most engineering work.

Coordinate transforms and reference systems

Tile math

Lat/lon to tile (z/x/y)

Given a lat/lon and zoom level z, find which slippy-map tile contains that point. This is the math behind every URL like `tile.openstreetmap.org/{z}/{x}/{y}.png`.

Tile math — z, x, y and the slippy map convention

Tile (z/x/y) to lat/lon (NW corner)

Recover the lat/lon of a tile's northwest corner. Combined with the tile to its southeast, this gives the full geographic bounding box of a tile.

Tile math — z, x, y and the slippy map convention

Pixel resolution at zoom z

How many real-world metres each pixel represents at zoom z, at the equator. Halves with every zoom level. Useful for sizing markers, buffers, and accuracy thresholds in screen space.

Pixel and world coordinates

Polygons & geometry

Shoelace formula — planar polygon area

Computes the area of a polygon from its vertex coordinates, on a flat plane. Works for any simple polygon (no self-intersections). The signed version (without absolute value) tells you winding direction.

Polygons — area, centroid, and containment

Spherical polygon area

Area of a polygon on a sphere, accounting for Earth's curvature. Required for any polygon spanning more than ~100 km — planar shoelace gives wrong answers at country/continent scale.

Polygons — area, centroid, and containment

Polygon centroid (planar)

The geometric center of mass of a polygon (not the average of vertices, which is wrong for irregular shapes). Useful for label placement, cluster centers, and pivot points.

Polygons — area, centroid, and containment

Point in polygon — ray casting

Cast a ray from the test point to infinity in any direction; count how many polygon edges it crosses. Odd = inside, even = outside. Simple, robust, and the standard implementation in turf.js and most GIS libraries.

Polygons — area, centroid, and containment

Spatial indexing

Geohash precision

Geohash encodes lat/lon as a short base-32 string. Each character adds 5 bits of precision, alternating between latitude and longitude. Cell size shrinks rapidly with length:

LengthCell size (mid-latitudes)Typical use
1~5,000 km × 5,000 kmcontinents
4~40 km × 20 kmcities
6~1.2 km × 600 mneighbourhoods
8~38 m × 19 mbuildings
10~1.2 m × 0.6 mparking spaces
→ Spatial indexing — Geohash, Quadtrees, S2, H3

3D & ECEF

Geodetic → ECEF (lat/lon/alt → X, Y, Z)

Convert lat/lon/altitude to Earth-Centered Earth-Fixed Cartesian coordinates. WGS84 parameters: a = 6,378,137 m, e² ≈ 0.006694. Used in GPS computations, satellite tracking, and any 3D Earth modeling where distance must be measured through the planet.

ECEF — 3D Earth-centered coordinates

ECEF Euclidean distance

Straight-line distance through the Earth between two ECEF points. For airline distance use Haversine (great-circle along the surface); ECEF distance is shorter because it cuts through the planet.

ECEF — 3D Earth-centered coordinates