Skip to main content

Function Route()

Use Route() to calculate traveling distance and time between any locations by car, truck, scooter, bike or on foot, with real-time or historic traffic and a custom departure time

Written by Erik

Use Route() to get the traveling distance and time for a route with two or more locations. Use the function's parameters to set the travel mode and the vehicle, to account for real-time or historic traffic, and to avoid some types of roads.

How to use

Use the function in any of the following ways:

  • Route(lat1, lng1, lat2, lng2)

  • Route(lat1, lng1, lat2, lng2, params)

  • Route(waypoints)

  • Route(waypoints, params)

Input

  • lat1, lng1, lat2, lng2 (decimal or text): latitude and longitude of the starting and ending points of the route, in decimal degrees (e.g. 37.78570).

  • waypoints: a json-array of the route's points, each with lat and lng. Example value: [{"lat": 52.267700, "lng": 21.060480}, {"lat": 52.201282, "lng": 20.840765}]
    ​Note. When optimize is true, the first and last points in waypoints are not re-ordered, as they are the starting and finishing locations of the route (just like when building a route in the Mapsly user interface).

  • params: an object with any of the following routing parameters, e.g. {travelMode: "truck", height: 4.0}. All of them are optional. Parameter names are case-sensitive: an unknown parameter (e.g. travelmode) or an unknown value makes the formula fail; it is not ignored.

    • travelMode (text): any of the following values (if omitted, Mapsly uses car):

      • car: by car

      • car_avoid_toll, car_avoid_ferry, car_avoid_motorway: by car, avoiding toll roads, ferries or motorways

      • small_truck: by small truck

      • truck: by full-size truck (see Truck parameters below)

      • scooter: by scooter

      • bike: by bicycle

      • foot: on foot

      The values used in earlier versions of this article (fastest, shortest, truck_fastest, truck_shortest, bicycle, pedestrian) still work. truck_balanced no longer exists: a formula that uses it fails.

    • optimize (boolean): false (default, when omitted) or true. When true, Mapsly re-orders the waypoints between the first and the last one to build a shorter route.

    • useTraffic (boolean): true or false (default, when omitted). When true, Mapsly accounts for traffic when building the route:

      • if the startAt parameter is omitted, Mapsly uses the current (real-time) traffic data;

      • if the startAt parameter is present, Mapsly uses historic traffic data to forecast traffic at the indicated day and time.

    • startAt (text): the departure date and time. Applicable only when useTraffic is true. You may indicate it in either of the two formats:

      • as a standard date and time that includes the timezone: 2010-04-27T12:00:00+02:00. To set a departure time relative to the current date and time, use the Now() function with the Twig date_modify filter and format the result as text: Now()|date_modify("+1 day")|date("c");

      • or as the local time at the route's starting location in the YYYY-MM-DD HH:MM format.

    • avoid: a json-array of road types that should be avoided. A road type can be any of the following values: tunnel, dirtRoad, boatFerry, railFerry, motorway, tollroad. This is a "soft" requirement: if there's no alternative route that avoids these types of roads, Mapsly may return a route that includes them.

Truck parameters

For truck, truck_fastest and truck_shortest you can describe the vehicle in params:

  • truckType: truck (a straight truck) or tractorTruck

  • trailersCount: 0 to 4

  • axleCount: 2 to 254

  • limitedWeight: the total weight, in tonnes

  • weightPerAxle: in tonnes

  • height, width, length: in metres

  • shippedHazardousGoods: a json-array of any of: combustible, corrosive, explosive, flammable, gas, harmfulToWater, organic, other, poison, poisonousInhalation, radioActive

  • tunnelCategory: B, C, D or E

With truck, any parameter you leave out takes its default: a straight truck of 26 t, 12 m long, 3.7 m high and 2.94 m wide, with 3 axles. truck_fastest and truck_shortest without these parameters follow truck-only road rules but ignore the truck's size and weight.

Output

The function returns an object with the following fields. Access them by name, e.g. Route(...).success, Route(...).distance_km.

  • success (boolean): true if the route has been built successfully, false if it hasn't been

  • message (text): a readable message describing why the route hasn't been built. Empty when success is true.

  • distance_km (decimal): the route's traveling distance in kilometers

  • distance_mi (decimal): the route's traveling distance in miles

  • time (decimal): the route's traveling time in minutes, not rounded (use |round to round it)

When the route can't be built, success is false, distance_km and distance_mi are 99999 and time is empty, so check success before using the numbers.

Limits

Each Route() call counts against your account's daily and per-session route-call limits, including repeated identical calls in the same automation run.

Examples

  • Route(37.78570, -122.40106, record.mapsly_geo_lat, record.mapsly_geo_lng).distance_mi - returns the driving distance from the San Francisco Museum of Modern Art to the current record's location, with no account for traffic.

  • Route("37.78570", "-122.40106", record.mapsly_geo_lat, record.mapsly_geo_lng, {"useTraffic": true, "startAt": Now()|date_modify("+1 day")|date("c")}).time - returns the driving time from the San Francisco Museum of Modern Art to the current record's location based on the traffic usual for the current time of day, on the day after today.

  • Route([{lat: 37.78570, lng: -122.40106}, {lat: record.mapsly_geo_lat, lng: record.mapsly_geo_lng}], {travelMode: "bike"}).time - returns the cycling time from the San Francisco Museum of Modern Art to the current record's location.

  • Route(record.depot_lat, record.depot_lng, record.mapsly_geo_lat, record.mapsly_geo_lng, {travelMode: "truck", height: 4.0, limitedWeight: 40}).time - returns the driving time for a 40 t truck that is 4 m high, from the depot to the current record's location (depot_lat and depot_lng stand for your own coordinate fields).

See also

Did this answer your question?