ForHosting KIT · Developer Utilities

Spherical law of cosines distance calculator

The spherical law of cosines distance calculator finds the shortest path along a spherical Earth between two latitude and longitude points.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

Enter both coordinate pairs in decimal degrees to receive the central angle and distance in kilometers, miles, and meters. It is a compact alternative to the haversine formula and is especially convenient when a direct cosine expression suits an existing calculation, spreadsheet, lesson, or implementation. Results use a fixed mean Earth radius, making every request deterministic and easy to reproduce.

Enter and interpret the coordinate pairs

Provide the latitude and longitude of the starting point as lat1 and lng1, then provide the destination as lat2 and lng2. Values must be decimal degrees: latitude runs from -90 at the South Pole to 90 at the North Pole, while longitude runs from -180 to 180. Negative latitude identifies the Southern Hemisphere, and negative longitude conventionally identifies positions west of Greenwich. The calculator returns the central angle in degrees together with kilometers, miles, and meters. The distance is the shorter great-circle arc over the surface of the model sphere, not a straight line through Earth. Reversing the points produces the same distance. Identical coordinates produce zero, subject only to the limits of floating-point arithmetic. Check coordinate order carefully because swapping a latitude with a longitude can still create syntactically valid numbers while identifying a completely different location. Keep enough decimal places for your intended scale: fewer decimals describe a less precise position even though the formula itself remains deterministic.

How the spherical law of cosines works

The method converts both latitudes and the longitude difference from degrees to radians. It then computes the cosine of the central angle using sin(latitude one) times sin(latitude two), plus cos(latitude one) times cos(latitude two) times cos(longitude difference). Applying arccosine gives the angular separation at Earth’s center. Multiplying that angle in radians by the fixed mean radius of 6,371 kilometers gives the surface distance. The same result is converted to meters and statute miles for convenience. Floating-point calculations can occasionally produce a cosine infinitesimally above 1 or below -1, especially for coincident or nearly antipodal points. The implementation clamps that intermediate value to the valid arccosine interval, preventing an otherwise valid calculation from becoming not-a-number. Output rounding is stable for repeatable API fixtures and comparisons. Unlike an ellipsoidal geodesic solver, this formula deliberately treats Earth as a sphere, so it favors clarity and compactness over survey-grade modeling of Earth’s flattening.

Choose this method for the right accuracy needs

Use spherical law-of-cosines distance for education, quick geographic comparisons, map summaries, routing estimates, dataset checks, and applications where a spherical approximation is appropriate. It is easy to audit because the calculation is a single familiar trigonometric identity. For most ordinary point separations, it gives a practical great-circle estimate comparable in purpose to the haversine method. At extremely short separations, the arccosine step can lose numerical precision because the cosine is very close to one; haversine is traditionally preferred when tiny distances must remain well resolved. For surveying, legal boundaries, high-precision navigation, or long baselines where Earth’s ellipsoidal shape matters, use an ellipsoidal method such as Vincenty or a modern geodesic library instead. Also remember that this result ignores terrain, road networks, buildings, restricted areas, currents, and flight procedures. It measures idealized surface separation, not a trip itinerary. Record the radius assumption when comparing this output with another tool, since two spherical calculators using different Earth radii can legitimately return slightly different distances.

Check geographic datasets

Compute a reproducible spherical separation between coordinate pairs while validating location records or detecting unexpectedly distant matches.

Teach great-circle geometry

Show how latitude, longitude difference, central angle, and arc length connect through a compact trigonometric identity.

Estimate point-to-point range

Get a fast approximate distance for maps, dashboards, logistics screening, or travel comparisons that do not require route geometry.

What does the calculation cost?

The API price is $0.002 per request. The browser calculator is free to use on this page.

Which Earth radius does the calculator use?

It uses a fixed mean Earth radius of 6,371 kilometers for deterministic spherical results.

Is this the same as the haversine formula?

No. Both estimate great-circle distance on a sphere, but they use different trigonometric forms. Haversine generally behaves better for extremely small separations.

Does the result follow roads or flight routes?

No. It is the shortest idealized great-circle arc on the sphere and does not include routing constraints, terrain, or operational detours.

Why might another distance calculator disagree?

It may use a different spherical radius, an ellipsoidal Earth model, different rounding, or an actual travel route instead of a great-circle arc.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/geo/law-of-cosines-distance

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/geo/law-of-cosines-distance \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"lat1":40.7128,"lng1":-74.006,"lat2":51.5074,"lng2":-0.1278}'
{
  "lat1": 40.7128,
  "lng1": -74.006,
  "lat2": 51.5074,
  "lng2": -0.1278
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "geo.law_of_cosines_distance",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Read the full KIT documentation →