How to set dashed line on Leaflet?

Have you noticed the dashArray option for your vector layers?

Forked Plunker: http://plnkr.co/edit/KyHOkjytDJf1QjCO0Nyh?p=preview

As for the "Train line style", that would depend on what you mean, but if it involves more than 1 color, you would probably need some workaround (duplicate your line…)


This is an example of a polyline formatted as train track / railway road. Three polylines are drawn on top of each other using dashArray.

plTrainPost[0] = L.polyline(latlonPost, {color: 'black', weight: '5'});
plTrainPost[1] = L.polyline(latlonPost, {color: 'black', weight: '3',  dashArray: '20, 20', dashOffset: '0'});
plTrainPost[2] = L.polyline(latlonPost, {color: 'white', weight: '3', dashArray: '20, 20', dashOffset: '20'});
var CPrePostLayer = L.layerGroup(plTrainPost[0], plTrainPost[1], plTrainPost[2]]);
var overlayMaps = {"An-/Rückfahrt": CPrePostLayer};
L.control.layers(baseMaps, overlayMaps).addTo(mymap);

If you want the track to appear in black only, you will not need plTrainPost[1]. Otherwise select the color of that polyline.