Reputation: 5069
I have one function which creates poly line & adds that polyline to map
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");
}
}
my problem is that It adds line with same color every time.
I want to add poly lines with different colors every time.
So how can I change color of polyline dynamically.
Upvotes: 1
Views: 5200
Reputation: 62881
Expanding on viabhav shah's answer (which is correct), your total solution would be:
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 has the correct answer, so vote for his solution.
Upvotes: 0
Reputation: 5069
For changing color I am using random color generator function.
Use get_random_color() in place of '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;
}
Upvotes: 7