Back

Distance Matrix

Note: This service requires your project to have an associated billing information.

Make sure to ENABLE this Distance Matrix API in your google map console dashboard.
The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations. The API returns information based on the recommended route between start and end points, as calculated by the Google Maps API.

You can get the distance matrix from an orgin to a specified destination by calling the place by calling the GetDistanceMatrix method of the map object like this:

 map.GetDistanceMatrix(origin, destination, mode, unit, callback)

origin Object
Required. The latLng literal object of the origin
destination Object
Required. The latLng literal object of the destination
mode String
Optional. Can be "driving", "bicycling", "walking", or "transit". Default is "driving"
unit String
Optional. The unit of measurement use. Can be "metric" or "imperial". Default is "metric"
callback Function
Required. The function to be called when the result is ready. It will pass an error, distance and duration respectively.

Return. None

Check first if there is an error. If error is true, then the second argument of the callback function is the error message, otherwise it is the distance followed by duration. If you use metric (default) the distance (length) is in meter and duration (time) is in seconds.

Get Elevation Example

app.LoadPlugin("MapView");

function OnStart() {
    lay = app.CreateLayout("Linear", "VCenter, FillXY");
        
        var apiKey = "AIzaSyD2eHntKEmh272p3ac6YjWPbPkwo3m2mck";
        
        map = app.CreateMapView(apiKey, 0.9, 0.65, 7.0689323, 125.6114588, 13);
        map.SetOnReady(MapOnReady);
        lay.AddChild(map);

    app.AddLayout(lay);
}
function MapOnReady() {
    var orig = {lat: 7.0689323, lng: 125.6114588};
    var dest = {lat: 7.1689323, lng: 125.2114588};
    map.GetElevation(orig, dest, null, null, OnResult);
}
function OnResult(error, dist, dur) {
    if(error) app.ShowPopup("Error " + dist);
    else {
        app.ShowPopup("Distance: "+(dist/1000)+" km, Duration: "+(dur/3600)+" hours");
    }
}
Copy    Run