ForHosting KIT · Travel & geography

Nearest point on a great-circle segment

For a point at latitude 30, longitude 45 and a leg from 0, 0 to 0, 90, the closest point is 0, 45: halfway along and 30° of arc away. When the projection falls past an end, the answer stays at that waypoint. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Latitude
30
Longitude
45
Start lat
0
Start lon
0
End lat
0
End lon
90
You get
Location
segment
Fraction
0.5
Angular distance deg
30
Distance m
3,335,852.407005986

This nearest-point calculator finds the location on a spherical great-circle segment that is closest to a supplied latitude and longitude. 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.

From the full great circle to one finite leg

Every great-circle leg sits on a full circle that wraps around the globe. The calculator turns the three locations into points on a unit sphere, uses the two waypoints to define the plane of that circle and drops your point straight onto it. That projection lies on the infinite circle, not necessarily on your leg, so the calculator then compares arc lengths to see whether it falls on the shorter arc from start to end. If it does, you get the point and its fraction along the leg; if not, the answer is the nearer waypoint. The separation is reported both as an angle and as a distance on the chosen sphere.

When it helps, and the usual mistakes

Use it to snap a reported position to one air or sea leg without letting the result run beyond either waypoint, or to measure how far a location sits from a leg so you can rank legs or flag positions outside a permitted corridor. The most common mistake is reaching for a flat map formula, which drifts on long legs and near the poles. Another is mixing up the leg with the whole great circle: if you need the distance to the endless line, that is a different calculation. Finally, the decimal setting only rounds what you see, from zero to twelve places; the point is always chosen at full precision.

How the closest point stays on the leg

The calculator converts all three locations into three-dimensional unit vectors. 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.

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.

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.

Does it use a flat map formula?

No. The leg is the shorter great-circle arc between its two ends, not a straight line on a flat map and not a constant-bearing rhumb line. On long routes and at high latitudes, flat formulas can place the closest point noticeably wrong.

What happens when the closest point falls beyond the leg?

The closest point on the full great circle may lie beyond the leg. When that happens, the answer is moved to whichever end of the leg is nearer, so the result never extends past either waypoint.

What does the fraction along the segment tell me?

It shows how far along the leg the closest point sits, from zero at the start to one at the end. In the example above it is 0.5, so the closest point is exactly halfway between the two waypoints.

Can I use a different sphere radius?

Yes. The default is the mean Earth radius, and any other positive radius can be set to model another sphere or match an existing calculation. The radius changes the distance in metres, not the latitude and longitude of the closest point.

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.

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.

Describe the query location and the segment

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 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. Enter the query location as lat and lon, then identify the segment with start_lat, start_lon, end_lat, and end_lon. 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 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 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. 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. For polylines, call the capability once for each segment and retain the result with the smallest distance. 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. If two endpoint distances are exactly tied in an otherwise ambiguous case, the deterministic result favors the start, which keeps repeated runs stable.

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.

Read the full KIT documentation →