Skip to content

Oliv/leaflet-polycolor

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
November 17, 2022 10:31
November 16, 2022 16:15
December 18, 2017 18:11
December 18, 2017 18:11
December 18, 2017 18:11
November 21, 2022 12:53
December 18, 2017 18:11
November 16, 2022 16:28
November 21, 2022 12:53
November 21, 2022 12:53
November 16, 2022 16:15

leaflet-polycolor NPM version Build Status

Color each polyline segment separately

Warning

This module only works with leaflet in canvas rendering mode. Be sure to add preferCanvas: true to your map options.

Installation

npm

$ npm install --save leaflet-polycolor

browser

  • Download or clone this github project
  • Include dist/polycolor.min.js in your project

compile

  • Download or clone this github project
  • Run npm i in the project folder
  • Run npm run build

Demo

Demo

Usage

es6

import L from 'leaflet';
import leafletPolycolor from 'leaflet-polycolor';
leafletPolycolor(L);

const map = L.map('map', {
    center: [45.1834782, 5.7831946],
    zoom: 13
});

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar'}).addTo(map);

const latLngs = [[45.187273, 5.758124], [45.182772, 5.758516], [45.185767, 5.747106], [45.176569, 5.752082], [45.171863, 5.757120], [45.168354, 5.755178]];
const colors = ['rgb(20, 200, 100)', 'rgb(200, 100, 20)', null, null, 'rgb(20, 200, 100)', 'rgb(0, 0, 0)'];

const polyline = L.polycolor(latLngs, {
  colors: colors,
  useGradient: true,
  weight: 5
}).addTo(map);

normal include of the minified file

<script src="leaflet-polycolor.min.js"></script>
var leafletPolycolor = require('leaflet-polycolor');
leafletPolycolor.default(L);

var map = L.map('map', {
    center: [45.1834782, 5.7831946],
    zoom: 13,
    preferCanvas: true,
});

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}', {foo: 'bar'}).addTo(map);

var latLngs = [[45.187273, 5.758124], [45.182772, 5.758516], [45.185767, 5.747106], [45.176569, 5.752082], [45.171863, 5.757120], [45.168354, 5.755178]];
var colors = ['rgb(20, 200, 100)', 'rgb(200, 100, 20)', null, null, 'rgb(20, 200, 100)', 'rgb(0, 0, 0)'];

var polyline = L.polycolor(latLngs, {
  colors: colors,
  useGradient: true,
  weight: 5
}).addTo(map);

API

Factory L.polycolor(<LatLng[]> latlngs, <Polyline options> options?)

Options

  • colors <Array> : An array of rgb colors strings rgb(x,y,z)
  • useGradient <Boolean> : Use or not gradient to smooth colors. Defaults to true

If colors are undefined, the default color parameter is used. To leave default color, use null in colors. ['rgb(0, 0, 0)', null, 'rgb(0, 45, 120)']

License

MIT © Olivier Gasc