Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have one function which creates poly line & adds that polyline to map

function makeRoute(e)
    if(pointsSelection.length > 0)
        var firstpolyline = new L.Polyline(pointsSelection, {
        color: 'blue',
        weight: 5,
        smoothFactor: 1



        selection = [];
        pointsSelection = [];
        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
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.… – Snorfalorpagus Mar 12 '13 at 10:49
up vote 4 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

Your Answer


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.