Google api 谷歌API |距离矩阵

Google api 谷歌API |距离矩阵,google-api,google-distancematrix-api,Google Api,Google Distancematrix Api,我需要代码方面的帮助 首先,我的方法正确吗?我使用GoogleDistanceMatrix来获得真正的路线距离,而不仅仅是线性距离 我想使用地理坐标作为起点和终点地址,但它似乎不起作用: public double CalculateGoogleDistance(GeoCoordinate from, GeoCoordinate to) { DistanceMatrixRequest request = new DistanceMatrixRequest(); request.Key =

我需要代码方面的帮助

首先,我的方法正确吗?我使用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;
}

首先,您似乎在使用Google Maps API服务时使用了第三方库/存储库,因此我建议您只关注他们的官方文档,我将在回答您的问题时提供这些文档

正如他们在文章中提到的,尽管是可选的,默认的
交通模型是
最佳猜测
,这意味着结果将基于已知的历史交通状况和实时交通的最佳行程时间估计。您可以了解有关其他流量模型可选参数的更多信息

现在,关于获取“真实路线距离”,您只参考了实时结果,不包括历史交通量。要获得此信息,您需要指定截止到现在的
departureTime
,如文档中的示例所示:

drivingOptions: {
    departureTime: new Date(Date.now() + N),  // for the time N milliseconds from now.
    trafficModel: 'bestguess'
}
如您所见,这就是距离矩阵API的工作原理

注意:出发时间和交通模型也适用于方向API

有关web服务的web服务版本,请参见此链接