DRIVINGDISTANCE
Calculate the driving distance along real roads between two geographic coordinates.
Use DRIVINGDISTANCE when road distance matters: charging delivery fees based on the actual driving route, limiting your service area to a driving radius, or quoting jobs that depend on how far your team has to travel.
How it works
DRIVINGDISTANCE(lat1, lng1, lat2, lng2)
DRIVINGDISTANCE(lat1, lng1, lat2, lng2, unit)lat1- Latitude of the starting location (number between -90 and 90)lng1- Longitude of the starting location (number between -180 and 180)lat2- Latitude of the destinationlng2- Longitude of the destinationunit- Optional. Unit for the result: "m" (meters), "km" (kilometers), "mi" (miles), "ft" (feet). Defaults to "m"
Returns the distance as a number in the specified unit, measured along the driving route between the two points. Latitude comes first, longitude second.
DRIVINGDISTANCE follows actual driving routes. For straight-line "as the crow flies" distance, use DISTANCE. For the driving time between the same points, use DRIVINGDURATION.
How loading works
DRIVINGDISTANCE retrieves the route from the road network, so it behaves differently from other formula functions.
The first time a formula runs with a coordinate pair it has not seen before, the result is empty for a moment while the route is retrieved in the background. The formula then runs again automatically and shows the result.
Coordinate pairs the formula has already seen reuse the route that was retrieved, so repeat calculations are instant.
If a coordinate is still empty, for example because a customer has not filled it in yet, the function returns 0 instead of an error.
If there is no drivable route between the two points, the formula returns the error "DRIVINGDISTANCE found no driving route between these points".
Examples
Delivery fee based on driving distance
Charge €5 plus €0.50 per kilometer of actual road distance from your warehouse.
WAREHOUSE_LAT = 52.0907
WAREHOUSE_LNG = 5.1214
CUSTOMER_DISTANCE = DRIVINGDISTANCE(WAREHOUSE_LAT, WAREHOUSE_LNG, CUSTOMER_LAT, CUSTOMER_LNG, "km")
DELIVERY_FEE = 5 + (CUSTOMER_DISTANCE * 0.50)Calculates the delivery fee from the real driving route to the customer.
Service area check
Determine if a customer is within a 50km driving radius of your office.
DRIVING_DISTANCE = DRIVINGDISTANCE(OFFICE_LAT, OFFICE_LNG, CUSTOMER_LAT, CUSTOMER_LNG, "km")
IF(DRIVING_DISTANCE < 50, "We deliver", "Contact us for a quote")Checks the service area using the road route instead of a straight line, so customers just outside a straight-line radius who can be reached quickly are handled the way your drivers would actually experience it.
Distance in miles
Get the driving distance between two cities in miles.
DRIVINGDISTANCE(52.3676, 4.9041, 48.8566, 2.3522, "mi")Returns the driving distance in miles instead of meters.
Common mistakes
Swapping latitude and longitude - Latitude comes first, longitude second. Amsterdam is (52.37, 4.90) not (4.90, 52.37)
Expecting a result on the first render - The first calculation for a new coordinate pair is empty until the route loads. The formula fills itself in automatically
Forgetting unit quotes - Use
"km"notkmfor the unit parameterUsing a time or other non-distance unit - A unit such as "min" produces a formula error. Use a distance unit
Invalid coordinates - Latitude must be -90 to 90, longitude -180 to 180
Understanding units
Available distance units:
"m"- Meters (default)"km"- Kilometers"mi"- Miles"ft"- Feet
DRIVINGDISTANCE also accepts any other distance unit the CONVERT function supports. If the unit is not a distance unit, the formula returns an error.