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 withlatandlng. Example value:[{"lat": 52.267700, "lng": 21.060480}, {"lat": 52.201282, "lng": 20.840765}]
Note. Whenoptimizeistrue, the first and last points inwaypointsare 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 usescar):car: by carcar_avoid_toll,car_avoid_ferry,car_avoid_motorway: by car, avoiding toll roads, ferries or motorwayssmall_truck: by small trucktruck: by full-size truck (see Truck parameters below)scooter: by scooterbike: by bicyclefoot: on foot
The values used in earlier versions of this article (
fastest,shortest,truck_fastest,truck_shortest,bicycle,pedestrian) still work.truck_balancedno longer exists: a formula that uses it fails.optimize(boolean):false(default, when omitted) ortrue. Whentrue, Mapsly re-orders the waypoints between the first and the last one to build a shorter route.useTraffic(boolean):trueorfalse(default, when omitted). Whentrue, Mapsly accounts for traffic when building the route:if the
startAtparameter is omitted, Mapsly uses the current (real-time) traffic data;if the
startAtparameter 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 whenuseTrafficistrue. 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 theNow()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:MMformat.
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) ortractorTrucktrailersCount: 0 to 4axleCount: 2 to 254limitedWeight: the total weight, in tonnesweightPerAxle: in tonnesheight,width,length: in metresshippedHazardousGoods: a json-array of any of:combustible,corrosive,explosive,flammable,gas,harmfulToWater,organic,other,poison,poisonousInhalation,radioActivetunnelCategory:B,C,DorE
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):trueif the route has been built successfully,falseif it hasn't beenmessage(text): a readable message describing why the route hasn't been built. Empty whensuccessistrue.distance_km(decimal): the route's traveling distance in kilometersdistance_mi(decimal): the route's traveling distance in milestime(decimal): the route's traveling time in minutes, not rounded (use|roundto 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_latanddepot_lngstand for your own coordinate fields).
See also
CurrentRoute() - get the traveling distance and time between the base record and the current match candidate (when used in a 'Match record' action's Rating formula) or the matched record (when used in 'Match record' action's Match actions) without explicitly specifying coordinate fields.
DirectDistance() - get the direct distance between any two locations in your Mapsly org's distance units
CurrentDirectDistance() - get the direct distance between the base record and the match candidate (when used in a 'Match record' action's Rating formula) or the matched record (when used in 'Match record' action's Match actions) without explicitly specifying coordinate fields.
Geocode() - get the coordinates of an address, as well as cleansed address (with corrected typos and filled in missing components like zip code or county).
