@@ -783,6 +783,9 @@ $(window).on('load', function() {
783783 * Adds polylines to the map
784784 */
785785 function processPolylines ( p ) {
786+
787+ var lines = Array ( p . length ) ; // array to keep track of loaded geojson polylines
788+
786789 if ( ! p || p . length == 0 ) return ;
787790
788791 var pos = ( getSetting ( '_polylinesLegendPos' ) == 'off' )
@@ -814,20 +817,19 @@ $(window).on('load', function() {
814817 }
815818 }
816819
817- line = L . polyline ( latlng , {
820+ var line = L . polyline ( latlng , {
818821 color : ( p [ index ] [ 'Color' ] == '' ) ? 'grey' : p [ index ] [ 'Color' ] ,
819822 weight : trySetting ( '_polylinesWeight' , 2 ) ,
820823 pane : 'shadowPane'
821- } ) . addTo ( map ) ;
824+ } )
825+
826+ lines [ index ] = line ;
827+ line . addTo ( map ) ;
822828
823829 if ( p [ index ] [ 'Description' ] && p [ index ] [ 'Description' ] != '' ) {
824830 line . bindPopup ( p [ index ] [ 'Description' ] ) ;
825831 }
826832
827- polylinesLegend . addOverlay ( line ,
828- '<i class="color-line" style="background-color:' + p [ index ] [ 'Color' ]
829- + '"></i> ' + p [ index ] [ 'Display Name' ] ) ;
830-
831833 if ( index == 0 ) {
832834 if ( polylinesLegend . _container ) {
833835 polylinesLegend . _container . id = 'polylines-legend' ;
@@ -850,8 +852,15 @@ $(window).on('load', function() {
850852 }
851853 }
852854
853- if ( p . length == index + 1 ) {
855+ if ( lines . filter ( Boolean ) . length == p . length ) { // only if all polylines loaded
854856 completePolylines = true ;
857+
858+ // Add polylines to the legend - we do this after all lines are loaded
859+ for ( let j = 0 ; j < p . length ; j ++ ) {
860+ polylinesLegend . addOverlay ( lines [ j ] ,
861+ '<i class="color-line" style="background-color:' + p [ j ] [ 'Color' ]
862+ + '"></i> ' + p [ j ] [ 'Display Name' ] ) ;
863+ }
855864 }
856865 } ;
857866 } ( i ) ) ;
0 commit comments