How to determine if the location is set under specific radius
Here is an example of a TypeScript class that can be used to check if a given latitude and longitude point is within a specified radius: class LatLonRadiusChecker { private radius: number; construct
Anuj Pandey
Here is an example of a TypeScript class that can be used to check if a given latitude and longitude point is within a specified radius:
class LatLonRadiusChecker { private radius: number; constructor(radius: number) { this.radius = radius; } public isInRadius(lat: number, lon: number, centerLat: number, centerLon: number): boolean { // convert lat/lon to radians lat = lat * (Math.PI / 180); lon = lon * (Math.PI / 180); centerLat = centerLat * (Math.PI / 180); centerLon = centerLon * (Math.PI / 180); // calculate deltas const deltaLat = lat - centerLat; const deltaLon = lon - centerLon; // calculate the haversine formula const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) + Math.cos(centerLat) * Math.cos(lat) * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // calculate the distance in kilometers const distance = 6371 * c; return distance