my.calculateRoute
Call this API to calculate the route.
Notes:
- the map-related APIs are backed by Google Maps. These API calls are charged so you need to provide your own gAPIKey instead of using the super app's API key. Contact our technical support for details.
- The IDE simulator cannot obtain the return values. Use the real development environment to obtain the return values.
Parameters
Property | Type | Required | Description |
searchType | String | No | Indicates the mode of transportation. Valid values are:
Note: The default value is |
startLat | Number | Yes | Indicates the latitude of the starting point. |
startLng | Number | Yes | Indicates the longitude of the starting point. |
endLat | Number | Yes | Indicates the latitude of the endpoint. |
endLng | Number | Yes | Indicates the longitude of the endpoint. |
throughPoints | Array | No | For Google Maps: Indicates a set of points along the route. This parameter is not available when the value of searchType is |
gAPIKey | String | No | Indicates the API key required to call this API. To learn how to get gAPIKey, refer to API key. Notes:
|
API key
You can use your own API key when calling the map-related APIs backed by Google Maps. Follow the steps below for configuration:
- Get an API Key from Google: Follow Google's instructions to get an API key, which is required when using the Google Maps service.
- Encrypt your Google API key: To encrypt your Google API key, contact our technical support. Then replace
ENCRYPTED_CUSTOM_GOOGLE_API_KEY
with your encrypted Google API key:
this.mapCtx.showRoute({
data: "",
gAPIKey: "ENCRYPTED_CUSTOM_GOOGLE_API_KEY", // The API key must be encrypted
success: function (res) { },
fail: function (res) { }
});
Error codes
Google Maps
{
"success": false,
"errorCode": 1900,
"status": "INVALID_REQUEST",
"errorMessage": "Exactly two waypoints required in transit requests"
}
The default error code is 1900
(compatible with Gaode Map, which means the 1900
error code of Gaode Map is used to throw any error instance of Google Maps). For error details about Google Maps, see status and errorMessage. Note that status corresponds to DirectionStatus in Google Maps. To learn more, refer to Status of Directions Query on Google Maps Platform.
Success callback function
The incoming parameter is an object parameter with the following attributes:
Property | Type | Description |
success | Boolean | Specifies whether the API call is successful. When the value is |
distance | Number | Indicates the distance between the starting point and the endpoint. |
duration | Number | Indicates the length of time spent according to the planned route, in seconds. |
Sample code
//.js
my.calculateRoute({
searchType: "walk", // searchType: "walk", "bus", "drive", "ride". Default value: walk. Added in v10.1.50.
startLat: 1.339712, // The latitude of the starting point.
startLng: 103.855457, // The longitude of the starting point.
endLat: 1.342983, // The latitude of the end point.
endLng: 103.867935, // The longitude of the end point.
throughPoints: [{ lat: 1.343573, lng: 103.861916 }], // A set of points along the route.
success:(res)=>{
console.log(res.distance);
console.log(res.duration);
}
});
Sample of the success callback function
{
"distance": 328,
"duration": 262,
"success": true
}
Sample of the fail callback function
{
"success": false,
"errorCode": 1900,
"status": "INVALID_REQUEST",
"errorMessage": "Exactly two waypoints required in transit requests"
}