ForHosting KIT · Developer Utilities

Nearest point on a great-circle segment

This nearest-point calculator finds the location on a spherical great-circle segment that is closest to a supplied latitude and longitude.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

It projects the query location onto the segment's great circle, checks whether that projection lies between the two endpoints, and clamps the answer to the nearer endpoint when it does not. The response includes the nearest coordinates, the position classification, the fraction along the segment, and the spherical separation as both an angle and a distance.

Describe the query location and the segment

Enter the query location as lat and lon, then identify the segment with start_lat, start_lon, end_lat, and end_lon. Every coordinate uses decimal degrees: latitude runs from south to north between -90 and 90, while longitude runs west to east between -180 and 180. The segment is the shorter great-circle arc joining its endpoints, not a straight line on a flat map and not a constant-bearing rhumb line. This distinction matters on long routes and at high latitudes, where common planar projection formulas can place the nearest point noticeably incorrectly. The default sphere radius is the IUGG mean Earth radius. You may supply a different positive radius when modeling another sphere or matching an established calculation, without changing the returned nearest latitude and longitude. The optional precision setting controls rounding of all numeric response fields; it does not reduce the internal precision used to select the point.

Understand projection and endpoint clamping

The calculator converts all three locations into three-dimensional unit vectors. The cross product of the endpoint vectors defines the plane of the great circle, and removing the query vector's component normal to that plane gives its perpendicular spherical projection. A projection on the infinite great circle is not automatically on the finite segment. The algorithm therefore compares angular arc lengths to determine whether the projected vector lies on the shorter arc from start to end. If it does, the response reports location as segment and supplies its fractional progress from zero at the start to one at the end. If it falls outside, the result is clamped to whichever endpoint has the smaller angular separation, and location becomes start or end. Coincident endpoints form a valid zero-length segment. Exactly antipodal endpoints are rejected because infinitely many great circles connect them, so the intended segment cannot be inferred from those two coordinates alone.

Use the result in geographic workflows

The nearest object is the coordinate to store, display, or feed into a later geographic calculation. The fraction is useful for locating an event along a route or interpolating metadata such as planned time, provided that metadata follows the same great-circle segment. The angular distance is independent of the chosen sphere radius, while distance_m is that angle multiplied by earth_radius_m. This is a spherical model, so it is appropriate for navigation estimates, global visualizations, route matching, and other tasks where a stable great-circle answer is desired. It is not an ellipsoidal geodesic solver and should not replace a surveyed terrestrial boundary calculation that requires a named datum and centimeter-level accuracy. For polylines, call the capability once for each segment and retain the result with the smallest distance. If two endpoint distances are exactly tied in an otherwise ambiguous case, the deterministic result favors the start, which keeps repeated runs stable.

Snap a position to an air or sea route

Find the closest valid location on one great-circle leg without allowing the projection to extend beyond either waypoint.

Measure separation from a route segment

Use the returned spherical distance to rank route legs or flag locations that lie outside a permitted corridor.

Place an event along a planned leg

Use the clamped fraction to associate a report with progress between two route endpoints.

What does an API request cost?

The API price is $0.002 per request. The same deterministic calculation can run in the browser on this page.

Does this use a flat map projection?

No. It performs vector and angular calculations on a sphere and follows the shorter great-circle arc between the endpoints.

What does clamping mean?

If the perpendicular projection lies beyond the finite segment, the calculator returns the closer segment endpoint instead.

What happens when both segment endpoints are identical?

The segment has zero length, so that shared endpoint is returned as the nearest point with a fraction of zero.

Why are antipodal endpoints rejected?

Opposite points on a sphere can be joined by infinitely many great circles. Two endpoints alone do not identify which arc was intended.

Is the calculation based on the WGS84 ellipsoid?

No. Coordinates use familiar latitude and longitude ranges, but distances and projections use a sphere with a configurable radius.

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/nearest-point-on-segment

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/nearest-point-on-segment \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"lat":30,"lon":45,"start_lat":0,"start_lon":0,"end_lat":0,"end_lon":90}'
{
  "lat": 30,
  "lon": 45,
  "start_lat": 0,
  "start_lon": 0,
  "end_lat": 0,
  "end_lon": 90
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "geo.nearest_point_on_segment",
  "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 →