{
  "id": "distance",
  "version": "2684ad347949",
  "status": "published",
  "name": "Distance Calculator",
  "question": "Distance between two places",
  "summary": "Finds the great-circle distance (as the crow flies) between two places from their latitude and longitude, in miles, kilometers, and nautical miles, with the initial compass bearing.",
  "category": "everyday-life",
  "subcategory": "travel-and-fuel",
  "url": "https://www.acalculator.org/everyday-life/distance-calculator",
  "markdown": "https://www.acalculator.org/everyday-life/distance-calculator.md",
  "kind": "function",
  "method": "a = sin²(Δφ ÷ 2) + cos φ1 × cos φ2 × sin²(Δλ ÷ 2); c = 2 × atan2(√a, √(1 − a)); distance = 6,371.0088 km × c",
  "assumptions": [
    "The Earth is a sphere with the mean radius of 6,371.0088 km; the real, slightly flattened Earth differs by under 1%.",
    "The distance is along the surface (as the crow flies), not by road.",
    "Latitudes are −90 to 90 degrees (north positive) and longitudes −180 to 180 degrees (east positive)."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "lat1": {
        "title": "From latitude",
        "description": "Latitude of the first place in decimal degrees; north is positive.",
        "type": "number",
        "minimum": -90,
        "maximum": 90
      },
      "lon1": {
        "title": "From longitude",
        "description": "Longitude of the first place in decimal degrees; east is positive, west negative.",
        "type": "number",
        "minimum": -180,
        "maximum": 180
      },
      "lat2": {
        "title": "To latitude",
        "description": "Latitude of the second place in decimal degrees; north is positive.",
        "type": "number",
        "minimum": -90,
        "maximum": 90
      },
      "lon2": {
        "title": "To longitude",
        "description": "Longitude of the second place in decimal degrees; east is positive, west negative.",
        "type": "number",
        "minimum": -180,
        "maximum": 180
      }
    }
  },
  "outputs": {
    "distance": {
      "label": "Distance",
      "description": "The great-circle distance: the shortest path along the Earth’s surface, on a sphere.",
      "format": "quantity",
      "unit": "mi"
    },
    "km": {
      "label": "In kilometers",
      "description": "The same distance in kilometers.",
      "format": "quantity",
      "unit": "km"
    },
    "nmi": {
      "label": "In nautical miles",
      "description": "The same distance in nautical miles (1,852 m).",
      "format": "quantity",
      "unit": "nmi"
    },
    "angle": {
      "label": "Central angle (degrees)",
      "description": "The angle between the two places seen from the Earth’s center, in degrees.",
      "format": "number"
    },
    "bearing": {
      "label": "Initial bearing (degrees)",
      "description": "The compass direction to set off in from the first place, in degrees clockwise from true north (0 to 360).",
      "format": "number"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "lat1": 33.95,
      "lon1": -118.4,
      "lat2": 40.6333,
      "lon2": -73.7833
    },
    "outputs": {
      "distance": 3972866.319659164,
      "km": 3972866.319659164,
      "nmi": 3972866.319659164,
      "angle": 35.728795836248466,
      "bearing": 65.89222059111842
    },
    "text": "From 33.95, -118.4 to 40.6333, -73.7833 is 2,468.6 mi as the crow flies."
  },
  "examples": [
    {
      "given": {
        "lat1": 33.95,
        "lon1": -118.4,
        "lat2": 40.63333333333333,
        "lon2": -73.78333333333333
      },
      "expect": {
        "angle": 35.728788667665434,
        "bearing": 65.89215179232762,
        "distance": 3972865.5225479994
      },
      "source": "Ed Williams, Aviation Formulary V1.47, worked example LAX to JFK: d = 0.623585 radians, initial course 1.150035 radians (https://edwilliams.org/avform147.htm); the source rounds to 6 decimals, so the tolerance is 1e-6",
      "tolerance": 0.000001
    },
    {
      "given": {
        "lat1": 0,
        "lon1": 0,
        "lat2": 90,
        "lon2": 0
      },
      "expect": {
        "angle": 90,
        "distance": 10007557.221017962,
        "bearing": 0
      },
      "source": "hand calculation in content.mdx: c = π ÷ 2; mean Earth radius 6,371,008.8 m (Moritz, 2000)"
    },
    {
      "given": {
        "lat1": 0,
        "lon1": 0,
        "lat2": 0,
        "lon2": 1
      },
      "expect": {
        "angle": 1,
        "km": 111195.0802335329,
        "bearing": 90
      },
      "source": "hand calculation in content.mdx: 1 degree of the equator is 2π × 6,371.0088 km ÷ 360 = 111.195 km"
    },
    {
      "given": {
        "lat1": 40,
        "lon1": 179.5,
        "lat2": 40,
        "lon2": -179.5
      },
      "expect": {
        "distance": 85179.92660565153,
        "bearing": 89.6786014074897
      },
      "source": "hand calculation in content.mdx; Python 3 haversine cross-check in docs/progress/WP-56.md"
    }
  ],
  "sources": [
    "Ed Williams, Aviation Formulary V1.47: the haversine distance and the initial course between two points, with the LAX to JFK example. https://edwilliams.org/avform147.htm",
    "H. Moritz, Geodetic Reference System 1980, Journal of Geodesy 74 (2000), 128–133: the mean Earth radius R1 = 6,371,008.8 m.",
    "NIST SP 811 (2008), Appendix B: 1 mi = 1.609344 km and 1 nautical mile = 1,852 m exactly."
  ],
  "related": [
    "gas",
    "mileage",
    "gas-mileage"
  ],
  "changelog": []
}
