【问题标题】:Get distance of route based on google url using google api使用google api根据google url获取路线距离
【发布时间】:2020-08-13 15:50:41
【问题描述】:

我想获得distance,最终通过google API 请求其他信息。我得到了谷歌API key。正如您在下面看到的,我有在程序运行期间填写的通用模板,其中可能有航点和旅行模式。通过JSON 获取距离和其他信息的google API 请求应该是什么?

String mapsUrl = $"https://www.google.com/maps/dir/?api=1&origin={origin}&destination={destination}&waypoints={torun}|{lodz}&travelmode={travelMode}&dir_action=navigate";

【问题讨论】:

    标签: c# google-maps google-api


    【解决方案1】:

    来自here 的 Google 文档 - 您需要使用距离矩阵 API。

    请求将类似于:

    https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=YOUR_API_KEY
    

    用您的密钥替换 YOUR_API_KEY 占位符。

    响应将是这种格式的 Json:

    {
       "destination_addresses" : [ "New York, NY, USA" ],
       "origin_addresses" : [ "Washington, DC, USA" ],
       "rows" : [
          {
             "elements" : [
                {
                   "distance" : {
                      "text" : "225 mi",
                      "value" : 361715
                   },
                   "duration" : {
                      "text" : "3 hours 49 mins",
                      "value" : 13725
                   },
                   "status" : "OK"
                }
             ]
          }
       ],
       "status" : "OK"
    }
    

    如果您没有正确的 API 密钥,请执行以下操作:

    1. 登录您的Google Cloud Platform Console
    2. 添加您的项目并添加 API。请注意,距离 Matrix API 不是免费的。
    3. 完成此操作后,您将获得 API 密钥。

    编辑

    如果您需要添加多个航点,则需要使用不同的 API,请参阅 documentation Maps JavaScript API 路线服务。简而言之,它与谷歌地图相同。

    它允许您添加一组航点。同样,它不是免费的 - 它是一种现收现付服务。

    DirectionsResult 对象将包含 Directions Legs(距离以米为单位)

    【讨论】:

    • 1.如果有路标怎么办? 2. 我得到了 google API - 使用/或如何计算此距离矩阵 API 时的价格是多少?
    • 您需要的所有信息都在他们的文档中:价格、如何使用等。我所看到的 DM Api 基于两点。
    • 所以没有办法基于两个以上的点来测量距离? :O
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多