【问题标题】:How to return distance in "Km" using Google Distance Matrix API?如何使用 Google 距离矩阵 API 以“公里”返回距离?
【发布时间】:2016-07-29 04:37:44
【问题描述】:

我正在使用 Google 的 Java 距离矩阵 API。 我有正常工作的代码,但我希望它以公里为单位返回两个经纬度点(起点、终点)之间的距离。但是没有这样的方法或参数可以以公里为单位返回距离但是,如果距离值大于或等于 1 公里,则返回以公里为单位的距离。但我希望它始终以公里为单位返回距离(例如 0.5 公里),但它总是在距离小于 1 公里时以米为单位返回距离。我也尝试使用Unit.METRIC,但它没有用,因为它仍然只返回以米为单位的距离。

DistanceMatrix results =  DistanceMatrixApi.getDistanceMatrix(context, 
                    new String[] {origins}, new String[] {destinations}).units(Unit.METRIC).await();

results.rows[0].elements[0].distance //returns in meters, i want it in kms.

【问题讨论】:

  • 除以 1000 还不够好吗?
  • 不,因为我已经看到了一些结果,并且通过 google 的距离矩阵 api 文档,它们在“Kms”示例链接中返回距离值:[link][maps.googleapis.com/maps/api/distancematrix/…
  • 因此,如果 API 有时返回已经以 Kms 为单位的距离并且我按照您的建议将其除以或乘以任何数字(例如 1000),这一切都会出错。我觉得这不是解决方案。
  • 文档developers.google.com/maps/documentation/distance-matrix/intro 说“此单位系统设置仅影响距离字段中显示的文本。距离字段还包含始终以米表示的值。”
  • 明白。谢谢

标签: java api google-maps google-distancematrix-api


【解决方案1】:

Documentation,您必须添加units 参数:

https://maps.googleapis.com/maps/api/distancematrix/json?<b>units=metric</b>

【讨论】:

    【解决方案2】:

    在阅读了关于距离矩阵 api 的谷歌文档后,我终于得出结论,距离字段将包含始终以米表示的“值”。所以这个值需要除乘以 1000 以获得以“Kms”为单位的值。

    【讨论】:

    • 这似乎不再是这种情况,因为我在距离为 1 米时返回值 0
    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多