Google API |距离矩阵
Google API | Distance Matrix
我的代码需要帮助。
首先,我使用 GoogleDistanceMatrix 获取实际路线距离而不仅仅是直线距离的方法是否正确?
我想为起点和终点地址使用地理坐标,但它似乎不起作用:
public double CalculateGoogleDistance(GeoCoordinate from, GeoCoordinate to) {
DistanceMatrixRequest request = new DistanceMatrixRequest();
request.Key = "****";
request.Origins = new Location[] {
new Location(from)
};
request.Destinations = new Location[] {
new Location(to)
};
var response = GoogleApi.GoogleMaps.DistanceMatrix.Query(request);
return response.Rows.First().Elements.First().Distance.Value;
}
首先,您似乎在使用第 3 方 libraries/repositories 使用 Google 地图 API 服务,所以我建议您只遵循他们的官方文档,其中我将在我回答您的问题时提供。
正如他们在 official documentation, even though it is optional, the default trafic_model
is bestguess
, meaning that the result will be based on the best estimate of travel time given what is known about both historical traffic conditions and live traffic. You can learn more about the other traffic model optional parameters here.
中提到的
现在,关于获取“真实路线距离”,您指的只是实时结果,不包括历史流量。为此,您需要将 departureTime
指定为 now,如文档中的示例所示:
drivingOptions: {
departureTime: new Date(Date.now() + N), // for the time N milliseconds from now.
trafficModel: 'bestguess'
}
如您所见,这就是距离矩阵 API 的工作原理。
Note: The departure time and traffic model is also applicable to Directions API.
有关 Distance Matrix API Web 服务的 Web 服务版本,请参阅此 link。
我的代码需要帮助。
首先,我使用 GoogleDistanceMatrix 获取实际路线距离而不仅仅是直线距离的方法是否正确?
我想为起点和终点地址使用地理坐标,但它似乎不起作用:
public double CalculateGoogleDistance(GeoCoordinate from, GeoCoordinate to) {
DistanceMatrixRequest request = new DistanceMatrixRequest();
request.Key = "****";
request.Origins = new Location[] {
new Location(from)
};
request.Destinations = new Location[] {
new Location(to)
};
var response = GoogleApi.GoogleMaps.DistanceMatrix.Query(request);
return response.Rows.First().Elements.First().Distance.Value;
}
首先,您似乎在使用第 3 方 libraries/repositories 使用 Google 地图 API 服务,所以我建议您只遵循他们的官方文档,其中我将在我回答您的问题时提供。
正如他们在 official documentation, even though it is optional, the default trafic_model
is bestguess
, meaning that the result will be based on the best estimate of travel time given what is known about both historical traffic conditions and live traffic. You can learn more about the other traffic model optional parameters here.
现在,关于获取“真实路线距离”,您指的只是实时结果,不包括历史流量。为此,您需要将 departureTime
指定为 now,如文档中的示例所示:
drivingOptions: {
departureTime: new Date(Date.now() + N), // for the time N milliseconds from now.
trafficModel: 'bestguess'
}
如您所见,这就是距离矩阵 API 的工作原理。
Note: The departure time and traffic model is also applicable to Directions API.
有关 Distance Matrix API Web 服务的 Web 服务版本,请参阅此 link。