这里的方向REST API

时间:2016-06-06 18:08:00

标签: json rest leaflet geojson

我正在尝试使用 here 中的路由REST API,但API的响应对我来说没有多大意义。

当我调用此网址时

https://route.cit.api.here.com/routing/7.2/calculateroute.json?waypoint0=51.5141%2C-0.0999&waypoint1=51.5081%2C-0.0985&mode=fastest%3Bpedestrian&app_id=DemoAppId01082013GAL&app_code=A...

我收到类似

的回复
{
"response": {
    "metaInfo": {
        "timestamp": "2016-06-06T17:40:58Z",
        "mapVersion": "8.30.62.155",
        "moduleVersion": "7.2.68.0-14232",
        "interfaceVersion": "2.6.24"
    },
    "route": [{
        "waypoint": [{
            "linkId": "-25305763",
            "mappedPosition": {
                "latitude": 51.5140062,
                "longitude": -0.0998158
            },
            "originalPosition": {
                "latitude": 51.5141,
                "longitude": -0.0999
            },
            "type": "stopOver",
            "spot": 0.6708861,
            "sideOfStreet": "right",
            "mappedRoadName": "St Paul's Churchyard",
            "label": "St Paul's Churchyard",
            "shapeIndex": 0
        }, {
            "linkId": "-1113519286",
            "mappedPosition": {
                "latitude": 51.5082735,
                "longitude": -0.098473
            },
            "originalPosition": {
                "latitude": 51.5081,
                "longitude": -0.0985
            },...

这看起来不像我可以在leaflet地图上显示的内容(即json中的polyline或类似内容)。

我如何解释?我如何获得可以在地图上显示的行?

1 个答案:

答案 0 :(得分:0)

从该数据结构中提取纬度 - 经度对并构建L.Polyline的实例非常简单:

var latlngs = [];

for (var i in json.response.route[0].waypoint) {
    var p = json.response.route[0].waypoint[i].mappedPosition;
    latlngs.push(L.latlng(p.latitude, p.longitude));
}

var line = L.polyline( latlngs );