Newer Version Available

This content describes an older version of this product. View Latest

Get the Distance Matrix

The GetDistanceMatrix() Apex method takes a set of locations and returns the estimated travel distance in meters and the time in seconds between the locations.

Example

With two locations A and B, you can use this method to return matrices of travel distance and times between A–B and B–A. The diagonal elements of the matrices A–A and B–B are zero because the travel distance and the time from one spot to the same spot is zero.

The values for A–B and B–A can differ because the returning path isn’t always the same. The travel times are calculated for eight predefined time windows based on historical traffic data. Each time window has its own travel time matrix.

Signature

Where,
  • maps is the namespace that's available after you install Salesforce Maps.
  • API is the class that contains the global methods exposed to developers.
  • GetDistanceMatrix() is the method.

Allocations

Include as many as 20 locations. To catch exceptions, put a try-catch block around your code.

Sample Code

This code finds the estimated travel distance in meters and time between two grocery stores.

If you invoke methods within a flow, process builder, or trigger, do one of the following to avoid uncommitted work errors.

  • Call the methods through a future method
  • Call the methods as queueable

Different processes refresh the token, such as plotting a route or schedule. The refresh process is almost immediate after each qualifying action occurs.

Warning

Sample Response

Although the return value is an Apex Map<String, Object> object, this JSON response illustrates the essential data that you receive in the resulting map.