This function is applicable to high concurrency scenarios, for example, online ride-hailing order dispatch. In this scenario, the function can calculate routes between multiple start points and end points, and find the start point and end point required for online ride-hailing order dispatch.
Protocol | HTTPS POST |
|---|---|
Direction | Your server -> Navi Kit server |
URL | https://mapapi.cloud.huawei.com/mapApi/v1/routeService/drivingMatrix?key=API KEY NOTE:
|
Data Format | Request: Content-Type: application/json Response: Content-Type: application/json |
Parameter | Mandatory | Type | Description |
|---|---|---|---|
key | Yes | String | Authentication key. For details about how to obtain one, please refer to Obtaining the API Key. |
Request Body
Parameter | Mandatory | Type | Description |
|---|---|---|---|
origins | Yes | Longitudes and latitudes of the start points. A maximum of 100 start points are allowed. | |
destinations | Yes | Longitudes and latitudes of the end points. A maximum of 100 end points are allowed. NOTE: The number of start points multiplying the number of end points must be less than 100. | |
avoid | No | int[] | Specified policy for calculating routes. The options are as follows:
The default value is 0. Currently, options 16, 32, 128, and 256 are supported only for route planning in the Chinese mainland. These options will be supported in other countries and regions later. |
departAt | No | long | Estimated departure time, in seconds since 00:00:00 on January 1, 1970 (UTC). The value must be the current time or a future time. |
trafficMode | No | int | Time estimation mode. The options are as follows:
The default value is 0. |
language | No | String(<=6) | Language of the returned result. Currently, only zh_CN (Chinese) and en (English) are supported. |
POST https://mapapi.cloud.huawei.com/mapApi/v1/routeService/drivingMatrix?key=API KEY HTTP/1.1 Content-Type: application/json Accept: application/json { "origins": [ {"lng": -1.894679, "lat": 54.974887}, {"lng": -2.882682, "lat": 54.857616} ], "destinations": [ {"lng": -1.744955, "lat": 54.775040} ], "avoid": [1,2], "trafficMode":2, "language": "en" }
When the status code is 200:
Response Header
Parameter | Mandatory | Type | Description |
|---|---|---|---|
Content-Type | Yes | application/json | Response data format. |
Response Body
Parameter | Type | Description |
|---|---|---|
returnCode | String | Result code. For details, please refer to Result Codes. |
returnDesc | String | Result description. |
originAddresses | String[] | Start point details. |
destinationAddresses | String[] | End point details. |
rows | Row[] | Distances and journey time of routes between start points and end points. |
When the status code is 200:
HTTP/1.1 200 OK
Content-type: application/json
{
"originAddresses": [
"12, Springfield, Prudhoe, Ovington, Northumberland, England, NE42 6EH, the United Kingdom",
"Saint Cuthbert Without, Carlisle, Cumbria, England, the United Kingdom"
],
"returnCode": "0",
"destinationAddresses": [
"Hedleyhope, Durham, England, DH7 9EY, the United Kingdom"
],
"rows": [
{
"cells": [
{
"duration": 2158.559,
"durationText": "35min",
"distance": 36129.102,
"distanceText": "36.1km",
"status": 0
}
]
},
{
"cells": [
{
"duration": 4856.301,
"durationText": "1h20min",
"distance": 104685.306,
"distanceText": "104.7km",
"status": 0
}
]
}
],
"returnDesc": "OK"
} public class MatrixService {
public static final String ROOT_URL = "https://mapapi.cloud.huawei.com/mapApi/v1/routeService/drivingMatrix";
public static final String connection = "?key=";
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static void matrixDriving(String apiKey) throws UnsupportedEncodingException {
JSONObject json = new JSONObject();
JSONArray origin = new JSONArray();
JSONObject startingPoint_1 = new JSONObject();
JSONObject startingPoint_2 = new JSONObject();
JSONArray destination = new JSONArray();
JSONObject endPoint = new JSONObject();
try {
startingPoint_1.put("lng", -1.894679);
startingPoint_1.put("lat", 54.974887);
startingPoint_2.put("lng", -2.882682);
startingPoint_2.put("lat", 54.857616);
origin.put(0, startingPoint_1);
origin.put(1, startingPoint_2);
endPoint.put("lng", -1.744955);
endPoint.put("lat", 54.775040);
destination.put(0, endPoint);
json.put("origins", origin);
json.put("destinations", destination);
json.put("language", "en");
} catch (JSONException e) {
Log.e("error", e.getMessage());
}
RequestBody body = RequestBody.create(JSON, String.valueOf(json));
OkHttpClient client = new OkHttpClient();
Request request =
new Request.Builder().url(ROOT_URL + connection + URLEncoder.encode(apiKey, "UTF-8"))
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("MatrixDriving", e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("MatrixDriving", response.body().string());
}
});
}
}