Skip to content

Distance Filter

The Distance Filter node filters rows by geographic proximity to a center point using the Haversine formula. It supports both kilometers and miles and automatically sorts results by distance (nearest first).

Configuration

PropertyTypeDefaultDescription
latFieldstringlatThe field name containing the row's latitude.
lngFieldstringlngThe field name containing the row's longitude.
centerLatnumber0Latitude of the center point.
centerLngnumber0Longitude of the center point.
maxDistancenumber50Maximum distance from the center point.
unitstringkmDistance unit: km (kilometers) or miles.

Input

Accepts a single object or an array of rows. Each row must have latitude and longitude fields.

Output

Returns rows within the specified distance, with an added _distance field (rounded to 2 decimal places). Results are sorted by distance ascending (nearest first).

json
[
  { "id": 1, "name": "Store A", "lat": 48.8566, "lng": 2.3522, "_distance": 0.42 },
  { "id": 2, "name": "Store B", "lat": 48.8606, "lng": 2.3376, "_distance": 1.15 }
]

Rows with lat: 0, lng: 0 are excluded from results.

How It Works

  1. For each input row, the node reads the latitude and longitude from the configured fields.
  2. It calculates the great-circle distance using the Haversine formula:
    • Earth radius: 6,371 km or 3,959 miles.
  3. Rows within maxDistance are kept; others are discarded.
  4. The _distance field is added to each surviving row.
  5. Results are sorted by _distance ascending.

The Haversine Formula

The Haversine formula calculates the shortest distance between two points on a sphere:

a = sin^2(dlat/2) + cos(lat1) * cos(lat2) * sin^2(dlng/2)
distance = radius * 2 * arcsin(sqrt(a))

This provides accurate distances for typical use cases (store locators, delivery zones).

Example Use Cases

Store locator within 25 km

latField: "latitude"
lngField: "longitude"
centerLat: 48.8566
centerLng: 2.3522
maxDistance: 25
unit: "km"

Delivery zone check (10 miles)

latField: "lat"
lngField: "lng"
centerLat: 40.7128
centerLng: -74.0060
maxDistance: 10
unit: "miles"

Dynamic center from user input

Use expressions in config to set the center from form inputs or upstream data:

centerLat: "`{ {input.user_lat}}`"
centerLng: "`{ {input.user_lng}}`"
maxDistance: 50
unit: "km"
Table Query (locations) --> Distance Filter (center from form) --> Limit (count: 20) --> Result

TIP

Store latitude and longitude as separate numeric columns in your database table. The Distance Filter works on in-memory data, so fetch all candidate rows first (with a generous bounding box if needed), then filter by distance.