如何将多段线添加到标记阵列中

How to add polyline to a marker array?

本文关键字:阵列 添加 段线      更新时间:2023-11-05

我对Leaflet完全陌生,我正在尝试在数据库中的标记之间添加多段线。我正在使用jQuery添加ajax响应中的标记(效果很好)。我已经阅读了文档,不知道如何添加多段线。这就是我尝试过的:

我的jQuery片段

    .success(function(response) {
        if(!response.errors && response.result) {
            $.each(response.result, function( index, value) {
                markerArray.push(L.marker([value[7], value[8]], {icon: greenIcon}));                
           });
             var group = L.featureGroup(markerArray).addTo(map);
             var polyline = L.polyline(markerArray, {color: 'red'}).addTo(map); 
             map.fitBounds(group.getBounds());       
        } else {
            $.each(response.errors, function( index, value) {
                // add error classes
                $('input[name*='+index+']').addClass('error').after('<div class="errormessage">'+value+'</div>')
            });
        }
    }); 

您必须在折线构造函数中使用LatLng数组(此处使用的是L.Marker数组)

我建议:

   $.each(response.result, function( index, value) {
       var latlng = L.latLng(value[7], value[8]);
       markerArray.push(L.marker(latlng, {icon: greenIcon})); 
       latlngArray.push(latlng);               
   });
   var group = L.featureGroup(markerArray).addTo(map);
   var polyline = L.polyline(latlngArray, {color: 'red'}).addTo(map);