Distance Matrix

DistanceMatrixService

google.maps.DistanceMatrixService

一种服务,用于计算多个起点和终点间的距离。

通过调用 const {DistanceMatrixService} = await google.maps.importLibrary("routes") 访问。请参阅 Maps JavaScript API 中的库

DistanceMatrixService
DistanceMatrixService()
参数:无
创建 DistanceMatrixService 的新实例,可向 Google 服务器发送距离矩阵查询。
getDistanceMatrix
getDistanceMatrix(request[, callback])
参数
用于发出距离矩阵请求。

DistanceMatrixRequest 接口

google.maps.DistanceMatrixRequest接口

DistanceMatrixService 发送的距离矩阵查询,其中包含起点和终点位置数组,以及各种计算指标选项。

destinations
类型Array<string|LatLng|LatLngLiteral|Place>
包含目的地地址字符串(即 LatLngPlace 对象)的数组,用作终点以计算距离和时间。必需。
origins
类型Array<string|LatLng|LatLngLiteral|Place>
包含出发地址字符串、LatLngPlace 对象的数组,将用作起点以计算距离和时间。必需。
travelMode
类型TravelMode
请求的路线类型。必需。
avoidFerries optional
类型boolean optional
如果为 true,则指示距离矩阵服务尽可能避开轮渡。可选。
avoidHighways optional
类型boolean optional
如果为 true,则指示距离矩阵服务尽可能避开高速公路。可选。
avoidTolls optional
类型boolean optional
如果为 true,则指示距离矩阵服务尽可能避开收费公路。可选。
drivingOptions optional
类型DrivingOptions optional
仅适用于 travelModeDRIVING 的请求的设置。此对象对其他出行方式没有任何影响。
language optional
类型string optional
返回结果所用语言的语言标识符(如果可能)。请参阅支持的语言列表
region optional
类型string optional
用作地址解析请求偏向的区域代码。地区代码接受 ccTLD(“顶级域名”)双字符值。多数 ccTLD 代码都与 ISO 3166-1 代码相同,但也有一些需要注意的例外情况。例如,英国的 ccTLD 为“uk”(.co.uk),而其 ISO 3166-1 代码为“gb”(特指“大不列颠及北爱尔兰联合王国”)。
transitOptions optional
类型TransitOptions optional
仅适用于 travelMode 为 TRANSIT 的请求的设置。此对象对其他出行方式没有任何影响。
unitSystem optional
类型UnitSystem optional
显示距离时要使用的首选单位制。可选;默认使用公制。

DistanceMatrixResponse 接口

google.maps.DistanceMatrixResponse接口

DistanceMatrixService 请求的响应,由具有格式的出发地和目的地地址以及一系列 DistanceMatrixResponseRow(每个相应的出发地地址)组成。

destinationAddresses
类型Array<string>
经过格式化的终点地址。
originAddresses
类型Array<string>
经过格式化的起点地址。
rows
矩阵的各行,对应起点地址。

DistanceMatrixResponseRow 接口

google.maps.DistanceMatrixResponseRow接口

DistanceMatrixService 请求的响应中的一行,由一系列 DistanceMatrixResponseElement 组成,每个相应的目标地址对应一个。

elements
该行的各元素,对应终点地址。

DistanceMatrixResponseElement 接口

google.maps.DistanceMatrixResponseElement接口

DistanceMatrixService 请求响应的单个元素,包含起点与终点间行程的时长和距离。

distance
类型Distance
该起点-终点对的距离。此属性可能未定义,因为距离可能未知。
duration
类型Duration
该起点-终点对的时长。此属性可能未定义,因为持续时间可能未知。
duration_in_traffic
类型Duration
该出发地-目的地配对的时长,考虑了 trafficModel 属性指示的流量条件。此属性可能为 undefined,因为时长未知。只有在发出请求时指定了 drivingOptions 的情况下,高级版客户才能使用。
fare
类型TransitFare
此出发地-目的地配对的总票价。仅适用于公交请求。
status
该特定起点-终点对的状态。

DistanceMatrixStatus 常量

google.maps.DistanceMatrixStatus 常量

通常在距离矩阵请求完成时,DistanceMatrixService 返回的关于该请求的顶级状态。可以按值或使用常量名称指定这些值。例如 'OK'google.maps.DistanceMatrixStatus.OK

通过调用 const {DistanceMatrixStatus} = await google.maps.importLibrary("routes") 访问。请参阅 Maps JavaScript API 中的库

INVALID_REQUEST 提供的请求无效。
MAX_DIMENSIONS_EXCEEDED 该请求包含的起点或终点的数量超过了 25 个。
MAX_ELEMENTS_EXCEEDED 这些起点和终点所产生的结果数量超出了每条查询的限制。
OK 响应中包含有效结果。
OVER_QUERY_LIMIT 在允许的时间段内请求的元素数量过多。如果您在一段时间后重试,请求应该会成功。
REQUEST_DENIED 该服务拒绝您的网页使用距离矩阵服务。
UNKNOWN_ERROR 由于服务器错误,系统无法处理距离矩阵请求。如果您重试一次,请求可能会成功

DistanceMatrixElementStatus 常量

google.maps.DistanceMatrixElementStatus 常量

当距离矩阵请求完成时,由 DistanceMatrixService 返回的特定起点-终点对的元素级状态。这些值以字符串形式指定,例如 'OK'

通过调用 const {DistanceMatrixElementStatus} = await google.maps.importLibrary("routes") 访问。请参阅 Maps JavaScript API 中的库

NOT_FOUND 无法对该配对的起点和/或终点进行地址解析。
OK 响应中包含有效结果。
ZERO_RESULTS 在原点和目标之间找不到任何路线。