目录

批量算路-驾车

功能介绍

提供多点之间驾车路径规划功能。

场景描述

适用于高并发场景,如网约车派单等场景,同时发起多个起终点之间的算路,筛选所需要的订单起终点。

使用约束

接口原型

承载协议

HTTPS POST

接口方向

开发者 -> 华为导航服务器

接口URL

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

说明:
  1. 获取API KEY的方式请参见获取API key。
  2. 使用API key时需要调用URLEncoder.encode("Your apiKey", "UTF-8")方法对API key进行encodeURI编码。例如:原始API key:ABC/DFG+ ,转换结果:ABC%2FDFG%2B。

数据格式

请求消息:Content-Type: application/json

响应消息:Content-Type: application/json

查询参数

参数

是否必选

参数类型

描述

key

是

String

鉴权秘钥,申请参见获取API key。

请求参数

Request Body

参数

是否必选

参数类型

描述

origins

是

Coordinate[]

起点的经纬度,不能超过100个。

destinations

是

Coordinate[]

终点的经纬度,不能超过100个。

说明:

起点数乘以终点数需不超过100。

avoid

否

int[]

表示计算出的路径应避免所指示的特性。取值包括:

  • 0:速度快
  • 1:避免收费
  • 2:避免高速
  • 4:距离短
  • 8:避免轮渡
  • 1,8:避免收费+避免轮渡
  • 16:躲避拥堵
  • 32:大路优先
  • 64:智能路线
  • 128:高速优先
  • 256:少收费

默认值为0,目前仅支持对中国大陆地区的路线规划支持“躲避拥堵"、“大路优先”、“高速优先”、“少收费”,后续将在其他区域陆续开放这些设置项。

departAt

否

long

预计出发时间。以自UTC 1970年1月1日午夜以来的秒数为单位。

必须是当前或者未来时间,不能是过去时间。

trafficMode

否

int

时间预估模型。取值包括:

  • 0:best guess
  • 1:路况差于历史平均水平
  • 2:路况优于历史平均水平

默认值为0。

language

否

String(<=6)

文字指引/描述的语种。目前只支持zh_CN和en。

请求示例

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

响应参数

状态码为200时:

Response Header

参数

是否必选

参数类型

描述

Content-Type

是

application/json

响应消息的数据格式。

Response Body

参数

参数类型

描述

returnCode

String

返回码,具体请参见错误码。

returnDesc

String

返回值描述。

originAddresses

String[]

Origins的地址详情。

destinationAddresses

String[]

Destinations的地址详情。

rows

Row[]

Origins和Destinations两两进行路径规划后的计算出来的时长和距离。

响应示例

状态码为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());
            }
        });
    }
}

Postman调试

您可以使用Postman在线调试此接口。