Skip to content

isWithinDistance

Whether a point lies within a radius of a center, the border included.

Available since v3.1.8

import { isWithinDistance } from '@helpers4/geography';
// or, from the all-in-one package (same code, one install):
import { isWithinDistance } from 'helpers4/geography';
isWithinDistance(centerLat: number, centerLon: number, radius: number, pointLat: number, pointLon: number): boolean
ParameterTypeDescription
centerLatnumberLatitude of the center, in degrees
centerLonnumberLongitude of the center, in degrees
radiusnumberThe radius in kilometres
pointLatnumberLatitude of the point, in degrees
pointLonnumberLongitude of the point, in degrees

boolean — true when the point is at most radius kilometres from the center

Radius check in kilometres, border included.

isWithinDistance(48.8566, 2.3522, 5, 48.8606, 2.3376)
// => true

Paris is not within 5 km of London.

isWithinDistance(48.8566, 2.3522, 5, 51.5074, -0.1278)
// => false

View source on GitHub