It's my first time using multiOptionsPolyline
to draw a multicolor polyline from an array of LatLng points, with red segments if distance is smaller or equal to 90m and green if bigger.
var polyLine = L.multiOptionsPolyline(latlngs, {
multiOptions: {
optionIdxFn: function (latLng, prevLatLng, index) {
//if (index == 0) return 0;
if (map.distance(latLng, prevLatLng) <= 90)
return 0;
return 1;
},
options: [{
color: 'red',
className: 'polyline-fill'
},
{
color: 'green',
className: 'polyline-nofill',
interactive: false
}
]
},
weight: 3
}).addTo(map);
If I have an array of elements var latlngs = [A, B, C, D, E, F, G, H, I, J]
and only D->E
(D-prevLatLng
and E-latLng
in optionIdxFn
function) is bigger than 90m, the segment between D and E should be green and the other red.
My only issue is that the green color is attached to the next segment (E->F), instead of the correct one and I cannot understand what am I doing wrong. The same thing happens even if I have, let's say D->E and E->F greater than 90m. E->F becomes green and F->G. The green color applies with an offset of one segment.
latlngs[index-1]
andlatlngs[index-2]
instead oflatLng
andprevLatLng
.latlngs[index]
andlatlngs[index+1]
(which should be easy to notice, since usingi
andi-1
shifted the colouring even further). I suggest you polish up plnkr.co/edit/7qx3YZ2JirwGhZyX , answer yourself, and submit a bug report & fix to the multioptionspolyline repo.