我有一个功能可以创建多线和&将折线添加到地图
function makeRoute(e)
{
if(pointsSelection.length > 0)
{
pointsSelection.push(e.target.getLatLng());
var firstpolyline = new L.Polyline(pointsSelection, {
color: 'blue',
weight: 5,
smoothFactor: 1
});
firstpolyline.addTo(map);
pointsArrayCollection.push(pointsSelection);
polyArrayCollection.push(firstpolyline);
selection = [];
pointsSelection = [];
}
else
{
alert("Please select more than one point");
}
}
我的问题是它每次都会添加相同颜色的线条。
我想每次添加不同颜色的多边形线。
那么如何动态改变折线的颜色呢。
答案 0 :(得分:5)
为了改变颜色,我使用随机颜色生成器功能。
使用get_random_color()代替'blue':
function get_random_color()
{
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ )
{
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
答案 1 :(得分:0)
扩展viabhav shah的答案(这是正确的),您的整体解决方案将是:
function makeRoute(e) {
if(pointsSelection.length > 0) {
pointsSelection.push(e.target.getLatLng());
var firstpolyline = new L.Polyline(pointsSelection, {
color: get_random_color(),
weight: 5,
smoothFactor: 1
});
firstpolyline.addTo(map);
pointsArrayCollection.push(pointsSelection);
polyArrayCollection.push(firstpolyline);
selection = [];
pointsSelection = [];
} else {
alert("Please select more than one point");
}
// Function to create a random color
function get_random_color() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.round(Math.random() * 15)];
}
return color;
}
}
Vaibhav Shah有正确的答案,所以投票支持他的解决方案。