Search code examples
javascriptleafletopenstreetmappolyline

OSM marker and polyline in same array


With this code I get all markers and popup with number of each on map.

 var polyline1 = [
        ['1', 44.772142, 17.208980],
        ['2', 44.774753, 17.207644],
        ['3', 44.773964, 17.199587],
        ['4', 44.770823, 17.199207],
        ['5', 44.771399, 17.195699],
    ];


    for (var i = 0; i < polyline1.length; i++) {
        var marker = L.marker([polyline1[i][1], polyline1[i][2]])
            .bindPopup(polyline1[i][0])
            .addTo(map);
    }

But it can't show me polyline here

  var polyline = L.polyline(polyline1, {
            color: 'red'
        })
        .addTo(map);

Second option is marker foreach, but then it can't show me number of marker

polyline1.forEach(function(LatLng) {
         L.marker(LatLng)
         .addTo(map);

Is there any way to combine something to work??? I strictly need polyline with popup or custom markers here.


Solution

  • the polyline wants to create the line with the number as lat.

    [lat,lng,alt]
    ['1', 44.772142, 17.208980]
    

    create a array only with latlngs:

     var polyline1 = [
            ['1', 44.772142, 17.208980],
            ['2', 44.774753, 17.207644],
            ['3', 44.773964, 17.199587],
            ['4', 44.770823, 17.199207],
            ['5', 44.771399, 17.195699],
        ];
    
    var latlngs = [];
    polyline1.forEach(function(data){
       latlngs.push([data[1],data[2]]);
    }
    
     var polyline = L.polyline(latlngs, {
                color: 'red'
            }).addTo(map);