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

Viewed 116

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 Answers

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 });
Related