使用Mapbox距离API的一个很好的例子

时间:2016-08-02 14:54:41

标签: mapbox

我正在尝试使用Mapbox计算两个位置之间的持续时间,但示例here不完整(至少我的经验有限)。我想使用服务器端Java连接到这个API,但是我甚至无法使用javaScript,Python或者只是在浏览器的地址栏中使用基本示例。

我可以使用这个url在我的浏览器中运行一个示例,并在我的API密钥中替换:

https://api.mapbox.com/geocoding/v5/mapbox.places/Chester.json?country=us&access_token=pk.my-token-value

但是,我无法使用距离API获得类似的示例。我能管理的最好的就是这样:

https://api.mapbox.com/distances/v1/driving/[13.41894,52.50055],[14.10293,52.50055]&access_token=pk.my-token-value.

但我不知道如何格式化我的坐标,因为我无法找到一个例子。

有没有人能够让这个工作。理想情况下,在Java中,但客户端JavaScript或有效的URL将是一个很好的开始。

我还应该补充一点,我无法让JavaScript或Python工作,因为它们依赖于documentation中没有引用的外部库!!

提前致谢。

2 个答案:

答案 0 :(得分:0)

看起来您可以提供2个或多个以分号分隔的坐标的列表:

https://api.mapbox.com/optimized-trips/v1/mapbox/driving/13.41894,52.50055;14.10293,52.50055?access_token=pk.your_token

返回:

{
    "code":"Ok",
    "waypoints":[
        {"distance":9.0352511932471,"name":"","location":[13.418991,52.500625],"waypoint_index":0,"trips_index":0},
        {"distance":91.0575241567836,"name":"Berliner Chaussee","location":[14.103096,52.499738],"waypoint_index":1,"trips_index":0}
    ],
    "trips":   
      [
        {"geometry":"}_m_Iu{{pA}ZuQ}Iad@}cAk^jr@etOdE_iGtTqoAxBkoGnOkiCjR_s@wJ_v@b@}aN|aBogRyVucBiEw_C~r@_eB`Fc`NtP_bAshBorHa@}dCkOe~AmPmrGlPlrGjOd~A`@|dCrhBnrHuP~aAaFb`N_s@~dBhEv_CxVtcBkbBbeRo@l`NzJ`z@mRpr@qOpjCwBpnGoT~lAeEdkGsr@jtOtp@dQ~UjTtDfZf]jS",
        "legs":[
            {"summary":"","weight":4198.3,"duration":3426.4,"steps":[],"distance":49487},
            {"summary":"","weight":7577.8,"duration":3501.3,"steps":[],"distance":49479.7}
        ],
        "weight_name":"routability",
        "weight":11776.1,
        "duration":6927.700000000001,
        "distance":98966.7}
      ]
}

答案 1 :(得分:0)

我不知道这现在是否更好,但我现在必须完成它并且可以给你这个例子

curl "https://api.mapbox.com/directions-matrix/v1/mapbox/driving/9.51416,54.47004;13.5096410,50.0716190;6.8614070,48.5206360;14.1304840,51.0856060?sources=0&access_token={token}"`

这将返回以下关于驾驶持续时间的 json。我使用了 sources 属性,这是我的搜索 lng/lat,所有其他点都在我的数据库中。

{
  "code": "Ok",
  "durations": [
    [0.0, 29407.9, 34504.7, 24163.5]
  ],
  "destinations": [{
    "distance": 131.946157371,
    "name": "",
    "location": [9.514914, 54.468939]
  }, {
    "distance": 34.636975593,
    "name": "",
    "location": [13.509868, 50.071344]
  }, {
    "distance": 295.206928933,
    "name": "",
    "location": [6.863566, 48.52287]
  }, {
    "distance": 1186.975749670,
    "name": "",
    "location": [14.115694, 51.080408]
  }],
  "sources": [{
    "distance": 131.946157371,
    "name": "",
    "location": [9.514914, 54.468939]
  }]
}

如果需要,将 annotations=distance 参数添加到 url 将返回距离而不是持续时间。

{
  "code": "Ok",
  "distances": [
    [0.0, 738127.3, 902547.6, 616060.8] // distances in meters
  ],
  "destinations": [{ // destinations including the source
    "distance": 131.946157371, // result 0
    "name": "",
    "location": [9.514914, 54.468939]
  }, {
    "distance": 34.636975593, // result 1
    "name": "",
    "location": [13.509868, 50.071344]
  }, {
    "distance": 295.206928933, // result 2
    "name": "",
    "location": [6.863566, 48.52287]
  }, {
    "distance": 1186.975749670, // result 3
    "name": "",
    "location": [14.115694, 51.080408]
  }],
  "sources": [{ // source where we start from
    "distance": 131.946157371,
    "name": "",
    "location": [9.514914, 54.468939]
  }]
}