Skip to main content

Calculate Distance

Calculates the straight-line (as the crow flies) distance between two geographic coordinates using the Haversine formula.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • Coordinate 1 - First geographic coordinate in "latitude,longitude" format (e.g., "41.0082,28.9784").
  • Coordinate 2 - Second geographic coordinate in "latitude,longitude" format (e.g., "40.7128,74.0060").

Output

  • Distance - The straight-line distance between the two coordinates in kilometers.

How It Works

The Calculate Distance node computes the straight-line distance between two geographic coordinates using the Haversine formula. When executed, the node:

  1. Validates both coordinate inputs
  2. Parses the latitude and longitude values from each coordinate string
  3. Applies the Haversine formula to calculate the great-circle distance
  4. Returns the distance in kilometers

The Haversine formula accounts for the Earth's curvature, providing accurate distance calculations between any two points on the globe.

Requirements

  • Two valid geographic coordinates in "latitude,longitude" format
  • Coordinates must be expressed as decimal degrees

Error Handling

The node will return specific errors in the following cases:

  • Empty or invalid Coordinate 1 input
  • Empty or invalid Coordinate 2 input
  • Incorrect coordinate format (missing comma or invalid numbers)
  • Non-numeric latitude or longitude values

Practical Examples

Example 1: Calculate Distance Between Two Cities

Find the straight-line distance between New York and Los Angeles:

Input:

  • Coordinate 1: "40.7128,-74.0060" (New York City)
  • Coordinate 2: "34.0522,-118.2437" (Los Angeles)

Output:

  • Distance: 3935.75 (kilometers)

Access result:

  • {{distance}} = approximately 3,936 km (2,445 miles)

Example 2: Find Nearest Store Location

Determine which store is closest to a customer:

Workflow:

  1. Set Variable - Customer location
    • {{customerCoords}} = "40.7589,-73.9851" (customer's coordinates)
  2. Set Variable - Store locations array
    {{stores}} = [
    { name: "Store A", coords: "40.7128,-74.0060" },
    { name: "Store B", coords: "40.7614,-73.9776" },
    { name: "Store C", coords: "40.7489,-73.9680" }
    ]
  3. Loop - Iterate through stores
  4. Calculate Distance - For each store
    • Coordinate 1: {{customerCoords}}
    • Coordinate 2: {{item.coords}}
  5. Set Variable - Store distance in array
    • {{item.distance}} = {{distance}}
  6. Find Minimum - Get store with smallest distance
    • Nearest store: Store with minimum {{distance}} value

Example 3: Proximity Filter for Search Results

Filter locations within a specific radius:

Scenario: Show only hotels within 10km of user location

  1. Address to Coordinates - Get user's coordinates
    • Address: {{userAddress}}
    • Output: {{userCoords}} = "{{result.geometry.location.lat}},{{result.geometry.location.lng}}"
  2. Read Database - Get all hotel locations
  3. Loop - Check each hotel
  4. Calculate Distance
    • Coordinate 1: {{userCoords}}
    • Coordinate 2: {{hotel.coordinates}}
  5. Condition - If {{distance}} 1 or less0
    • Add to results list
  6. Return - Filtered hotels within 10km

Example 4: Batch Distance Calculation from Excel

Calculate distances for multiple location pairs:

Excel Structure:

Origin LatOrigin LngDest LatDest LngDistance
40.7128-74.006034.0522-118.2437
51.5074-0.127848.85662.3522

Workflow:

  1. Read Excel - Load coordinate pairs
  2. Loop - Process each row
  3. Set Variable - Format coordinates
    • {{coord1}} = "{{item.originLat}},{{item.originLng}}"
    • {{coord2}} = "{{item.destLat}},{{item.destLng}}"
  4. Calculate Distance
    • Coordinate 1: {{coord1}}
    • Coordinate 2: {{coord2}}
  5. Set Variable - Store result
    • {{item.distance}} = {{distance}}
  6. Write Excel - Save with calculated distances

Example 5: Geofencing - Check if Location is Within Range

Verify if a GPS position is within allowed area:

Input:

  • Coordinate 1: "37.7749,-122.4194" (office location)
  • Coordinate 2: {{employeeGPS}} (employee's current location)

Workflow:

  1. Calculate Distance - Get distance from office
  2. Condition - Check if within geofence
    • If {{distance}} 0 or less.5 (500 meters)
      • Status: "On-site"
    • Else:
      • Status: "Remote"

Tips for Effective Use

Coordinate Format Requirements

  • Format: "latitude,longitude" as a single string
    • Correct: "40.7128,-74.0060"
    • Correct: "40.7128, -74.0060" (spaces are trimmed)
    • Incorrect: 40.7128,-74.0060 (missing quotes in some contexts)
    • Incorrect: "40.7128" and "-74.0060" (separate values)

Building Coordinate Strings

Combine latitude and longitude into the required format:

From separate variables:

// Using template literal
{{`${latitude},${longitude}`}}

// Using concatenation
{{latitude + "," + longitude}}

From geocoding result:

// After Address to Coordinates node
{{result.geometry.location.lat + "," + result.geometry.location.lng}}

Understanding Straight-Line vs Road Distance

  • Calculate Distance: Measures "as the crow flies" (straight line)

    • Faster calculation (no API call needed)
    • Good for proximity estimates
    • Does NOT account for roads, terrain, or actual travel
  • Get Route Distance: Measures actual driving distance

    • Requires Google API call
    • Accounts for roads and routes
    • More accurate for navigation and logistics

When to use Calculate Distance:

  • Quick proximity checks (e.g., "within 5km")
  • Sorting by nearest location
  • Geofencing applications
  • Initial filtering before detailed route calculation

When to use Get Route Distance:

  • Actual travel planning
  • Delivery route optimization
  • Accurate ETA calculations
  • Cost estimation based on distance traveled

Distance Unit Conversions

The output is always in kilometers. Convert if needed:

  • To Miles: {{distance * 0.621371}}
  • To Meters: {{distance * 1000}}
  • To Feet: {{distance * 3280.84}}
  • To Nautical Miles: {{distance * 0.539957}}

Accuracy Considerations

  • Haversine formula assumes Earth is a perfect sphere
  • Actual Earth is slightly ellipsoidal (oblate spheroid)
  • Typical accuracy: ±0.3% for most distances
  • Error increases for very long distances (over 1000km)
  • For most RPA applications, this accuracy is sufficient

Performance Benefits

This node does NOT require:

  • Google Maps API key
  • API quota usage
  • Network connectivity
  • External API calls

Benefits:

  • Extremely fast (local calculation)
  • No cost per request
  • Works offline
  • No rate limits
  • Ideal for batch processing

Common Errors and Solutions

Error: "ErrInvalidArg: Coordinate 1/2 cannot be empty"

Cause: Missing or empty coordinate input.

Solution:

  • Verify both coordinate variables have values
  • Check for null/undefined in your data
  • Use conditional logic to skip rows with missing coordinates

Error: "ErrInvalidArg: Coordinate 1/2 parse error"

Cause: Coordinate string is not in correct "lat,lng" format.

Solution:

  • Ensure format is "latitude,longitude"
  • Check for missing comma: "40.7128-74.0060"
  • Verify both parts are present: "40.7128,"
  • Ensure values are numeric: "forty,seventy"
  • Remove extra characters: "(40.7128,-74.0060)"

Error: "ErrInvalidArg: Coordinate 1/2 format error"

Cause: Coordinate doesn't have exactly 2 parts (lat and lng).

Solution:

  • Check for extra commas: "40.7128,-74.0060,100"
  • Ensure single comma separator: "40.7128,-74.0060"
  • Remove whitespace issues: Trim before formatting

Invalid Distance Results (Very Large or Negative)

Issue: Getting unexpected distance values.

Cause: Latitude and longitude might be swapped or out of range.

Solution:

  • Verify latitude is between -90 and 90
  • Verify longitude is between -180 and 180
  • Check if lat/lng are in correct order
  • Example correct: "40.7128,-74.0060" (lat first, lng second)

Coordinates from Different Sources Don't Match

Issue: Same location gives different distances when coordinates come from different sources.

Cause: Coordinate precision varies between sources.

Solution:

  • More decimal places = more precise
  • 6 decimal places ≈ 0.1 meter precision (sufficient for most uses)
  • Round coordinates to consistent precision if needed
  • Small variations (under 0.01km) are usually acceptable

Usage Notes

  • Coordinates should be provided in decimal degrees format
  • The distance is calculated as a straight line (not road distance)
  • The result is returned in kilometers
  • For coordinate formatting, use a comma separator: "latitude,longitude"
  • This calculation is useful for proximity checks and rough distance estimations
  • For driving distances, use the Get Route Distance node instead
  • No Google API key required - calculation is performed locally
  • Uses Haversine formula which accounts for Earth's curvature
  • Extremely fast and suitable for batch processing thousands of coordinates
  • Distance accuracy is typically within 0.3% for most practical purposes