目录

Driving Route Planning in Batches

Function

Plans driving routes between multiple places.

Scenario

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.

Restrictions

Prototype

Protocol

HTTPS POST

Direction

Your server -> Navi Kit server

URL

https://mapapi.cloud.huawei.com/mapApi/v1/routeService/drivingMatrix?key=API KEY

NOTE:
  1. For details about how to obtain an API key, please refer to Obtaining the API Key.
  2. You need to call the URLEncoder.encode("Your apiKey", "UTF-8") method to encode the API key using encodeURI. For example, if the original API key is ABC/DFG+, the conversion result is ABC%2FDFG%2B.

Data Format

Request: Content-Type: application/json

Response: Content-Type: application/json

Query Parameters

Parameter

Mandatory

Type

Description

key

Yes

String

Authentication key. For details about how to obtain one, please refer to Obtaining the API Key.

Request Parameters

Request Body

Parameter

Mandatory

Type

Description

origins

Yes

Coordinate[]

Longitudes and latitudes of the start points. A maximum of 100 start points are allowed.

destinations

Yes

Coordinate[]

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:

  • 0: fast.
  • 1: Avoid toll roads.
  • 2: Avoid highways.
  • 4: short distance.
  • 8: Avoid ferries.
  • 1,8: Avoid toll roads and ferries.
  • 16: Avoid congested roads.
  • 32: Prioritize main roads.
  • 64: Select routes intelligently.
  • 128: Prioritize highways.
  • 256: Take routes charging the least.

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:

  • 0: best guess
  • 1: The traffic condition is worse than the historical average.
  • 2: The traffic condition is better than the historical average.

The default value is 0.

language

No

String(<=6)

Language of the returned result. Currently, only zh_CN (Chinese) and en (English) are supported.

Request Example

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" 
}

Response Parameters

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.

Response Example

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"
}

Call Example

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());
            }
        });
    }
}

Debugging Using Postman

You can use Postman to debug this API online.