Newer Version Available
Get the Distance Matrix
The GetDistanceMatrix() Apex
method takes a set of locations and returns the estimated travel distance in meters and the
time 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 be different because the returning path isn’t always the same path.
The travel times are calculated for different time windows, so there’s a travel time matrix
for each time window.
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.
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.