Have a look at the demo. Links to source code are included.
L.multiOptionsPolyline(points, {
multiOptions: {
optionIdxFn: function (latLng) {
var i,
altThresholds = [800, 900, 1000, 1100, 1200, 1300, 1400, 1500];
for (i = 0; i < altThresholds.length; ++i) {
if (latLng.alt <= altThresholds[i]) {
return i;
}
}
return altThresholds.length;
},
options: [
{color: '#0000FF'}, {color: '#0040FF'}, {color: '#0080FF'},
{color: '#00FFB0'}, {color: '#00E000'}, {color: '#80FF00'},
{color: '#FFFF00'}, {color: '#FFC000'}, {color: '#FF0000'}
]
},
weight: 5,
lineCap: 'butt',
opacity: 0.75,
smoothFactor: 1}).addTo(map);
MIT-License (see LICENSE file).