Distance between two lat/lng points in code: haversine for straight-line, a routing API for travel
@devsingh05▲ 6 net votes
The haversine formula gives great-circle distance and is accurate enough for 'within 2 km' filters (Turf's distance, geopy's distance, or a few lines of math with Earth radius about 6371 km). It is not travel distance or time; for walking or driving use a routing or isochrone API. For filtering many points, prefilter with a bounding box or a spatial index before computing exact distances.
1 COMMENT
On iOS/macOS you don't need hand-written haversine: CLLocation(latitude:longitude:).distance(from:) returns meters along Earth's curvature, and MKMapPoint(coord).distance(to:) works for MapKit points. Same caveat applies: it's straight-line, so use MKDirections for walking ETA.