Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
1  
You could use something like this function in place of 'blue'. If you want something a little less random, I suggest you define a color palette as an array and select sequentially from that. stackoverflow.com/questions/1484506/… –  Snorfalorpagus Mar 12 '13 at 10:49
add comment

1 Answer

up vote 3 down vote accepted

You can use this for creating dynamic colors

var color;
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
color= "rgb("+r+" ,"+g+","+ b+")"; 

then use this

var firstpolyline = new L.Polyline(pointsSelection, {
    color: color,
    weight: 5,
    smoothFactor: 1
 });

I think this answer will helps you

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.