簡體   English   中英

如何使用 L.Routing.line?

[英]How to use L.Routing.line?

如何使用 L.Routing.line? 他總是報錯。 我想用 L.Routing.line 來繪制我從服務器發送到 map 的計算路徑信息 我服務器的數據如下:

{
    "paths": {
        "distance": 2.951884589808169,
        "weight": 0.3542261507769803,
        "time": 353,
        "ascend": 0.0,
        "descend": 0.0,
        "instructions": [{
            "distance": 1.537,
            "heading": 0.68,
            "sign": 0,
            "interval": [0, 1],
            "text": "繼續",
            "time": 184,
            "street_name": ""
        }, {
            "distance": 1.415,
            "sign": 2,
            "interval": [1, 2],
            "text": "右轉",
            "time": 169,
            "street_name": ""
        }, {
            "distance": 0.0,
            "sign": 4,
            "last_heading": 90.14917948588372,
            "interval": [2, 2],
            "text": "終點到達",
            "time": 0,
            "street_name": ""
        }],
        "points": [
            [38.871352, 115.452657],
            [38.871366, 115.452657],
            [38.871366, 115.452673]
        ]
    },
    "hints": {
        "empty": false
    }
}

我試着這樣寫:

   var waypoints = [];
   waypoints.push({latLng: L.latLng(path.points[0][0],path.points[0][1])});
    waypoints.push({latLng: L.latLng(path.points[path.points.length-1][0],path.points[path.points.length-1][1])});
    var coordinates=[];
    for(var i =0 ; i < path.points.length;i++){
        coordinates.push({latLng: L.latLng(path.points[i][0],path.points[i][1])});
    }
    var route = {
        name:"",
        summary: {
            totalDistance: path.distance,
            totalTime: path.time / 1000,
        },
        instructions: _convertInstructions(path.instructions),
        waypoints:waypoints,
        coordinates:coordinates
    }
    L.Routing.line(route).addTo(mymap);

錯誤:

     Uncaught TypeError: Cannot read property 'length' of undefined
            at i._findWaypointIndices (leaflet-routing-machine.js:16984)
            at i._getWaypointIndices (leaflet-routing-machine.js:17061)
            at i._extendToWaypoints (leaflet-routing-machine.js:17011)
            at initialize (leaflet-routing-machine.js:16967)
            at new i (leaflet.js:5)
            at Object.line (leaflet-routing-machine.js:16595)
            at Object.success (index:148)
            at d (jquery.min.js:2)
            at Object.fireWith [as resolveWith] (jquery.min.js:2)
            at S (jquery.min.js:4)

從我可以看到的錯誤中,
.length有問題,
其中path是您的 json,

Object.keys(path.points[0]).length

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM