How can I render the L.featureGroup from an array of coordinate

荒凉一梦 提交于 2020-06-12 15:23:49

问题


how can I render the L.featureGroup from an array of coordinateRoute in data.json, to handle with button "Snake it!", It's like when I click on "snake it!" has it's own id and the map will show Polyline of the coordinateRoute with corresponding id

Here's link of the code

Here's data.json:

link of data.json

tobe more Specifically this is how I try but WRONG:

    let route;
    data.map((item) => {
      if (item.id === 1 && item.coordinateRoute) {
    return (route = L.featureGroup([
      item.coordinateRoute.map((line) => {
        if (i % 2 === 0) {
          L.marker(item.coordinateRoute[i], { icon });
        }
        L.polyline([item.coordinateRoute[i], item.coordinateRoute[i + 1]]);
        return i++;
      }),
    ]));


        return (route = L.featureGroup(lines));
      }
      return route;
    });

回答1:


I made it !, but I don't think it is optimized

Here is my code:

let markerList = [];
    let polyLineList = [];
    let lines = [];
    for (let i = 0; i < 4; i++) {
      markerList.push(L.marker(data[0].coordinateRoute[i], { icon }));
      if (data[0].coordinateRoute[i + 1]) {
        polyLineList.push(
          L.polyline(
            [data[0].coordinateRoute[i], data[0].coordinateRoute[i + 1]],
            { snakingSpeed: 500 }
          )
        );
      }
    }
    let l = Math.min(markerList.length, polyLineList.length);

    for (let i = 0; i < l; i++) {
      lines.push(markerList[i], polyLineList[i]);
    }
 const route = L.featureGroup(lines, { snakingPause: 50 });


来源:https://stackoverflow.com/questions/62215914/how-can-i-render-the-l-featuregroup-from-an-array-of-coordinate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!