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.
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 friendsSpherical 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 friendsEquirectangular 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 friendsBearing & 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 sphereDestination 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 sphereProjections
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 globeWeb 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 globeUTM 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 systemsTile 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 conventionTile (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 conventionPixel 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 coordinatesPolygons & 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 containmentSpherical 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 containmentPolygon 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 containmentPoint 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 containmentSpatial 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:
| Length | Cell size (mid-latitudes) | Typical use |
|---|---|---|
| 1 | ~5,000 km × 5,000 km | continents |
| 4 | ~40 km × 20 km | cities |
| 6 | ~1.2 km × 600 m | neighbourhoods |
| 8 | ~38 m × 19 m | buildings |
| 10 | ~1.2 m × 0.6 m | parking spaces |
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 coordinatesECEF 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