Polyline
A class for drawing polyline overlays on a map. Extends Path
.
Usage example
// create a red polyline from an array of LatLng points
var latlngs = [
[45.51, -122.68],
[37.77, -122.43],
[34.04, -118.2]
];
var polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
// zoom the map to the polyline
map.fitBounds(polyline.getBounds());
You can also pass a multi-dimensional array to represent a MultiPolyline
shape:
// create a red polyline from an array of arrays of LatLng points
var latlngs = [
[[45.51, -122.68],
[37.77, -122.43],
[34.04, -118.2]],
[[40.78, -73.91],
[41.83, -87.62],
[32.76, -96.72]]
];
Creation
Factory | Description |
---|---|
L.polyline( |
Instantiates a polyline object given an array of geographical points and
optionally an options object. You can create a Polyline object with
multiple separate lines (MultiPolyline ) by passing an array of arrays
of geographic points. |
Options
Option | Type | Default | Description |
---|---|---|---|
smoothFactor |
Number
| 1.0 |
How much to simplify the polyline on each zoom level. More means better performance and smoother look, and less means more accurate representation. |
noClip |
Boolean
| false |
Disable polyline clipping. |
Option | Type | Default | Description |
---|---|---|---|
stroke |
Boolean
| true |
Whether to draw stroke along the path. Set it to false to disable borders on polygons or circles. |
color |
String
| '#3388ff' |
Stroke color |
weight |
Number
| 3 |
Stroke width in pixels |
opacity |
Number
| 1.0 |
Stroke opacity |
lineCap |
String
| 'round' |
A string that defines shape to be used at the end of the stroke. |
lineJoin |
String
| 'round' |
A string that defines shape to be used at the corners of the stroke. |
dashArray |
String
| null |
A string that defines the stroke dash pattern. Doesn't work on Canvas -powered layers in some old browsers. |
dashOffset |
String
| null |
A string that defines the distance into the dash pattern to start the dash. Doesn't work on Canvas -powered layers in some old browsers. |
fill |
Boolean
| depends |
Whether to fill the path with color. Set it to false to disable filling on polygons or circles. |
fillColor |
String
| * |
Fill color. Defaults to the value of the color option |
fillOpacity |
Number
| 0.2 |
Fill opacity. |
fillRule |
String
| 'evenodd' |
A string that defines how the inside of a shape is determined. |
renderer |
Renderer
|
|
Use this specific instance of Renderer for this path. Takes
precedence over the map's default renderer. |
className |
String
| null |
Custom class name set on an element. Only for SVG renderer. |
Option | Type | Default | Description |
---|---|---|---|
interactive |
Boolean
| true |
If false , the layer will not emit mouse events and will act as a part of the underlying map. |
Option | Type | Default | Description |
---|---|---|---|
pane |
String
| 'overlayPane' |
By default the layer will be added to the map's overlay pane. Overriding this option will cause the layer to be placed on another pane by default. |
Events
Event | Data | Description |
---|---|---|
click
| ||
dblclick
| ||
mousedown
| ||
mouseover
| ||
mouseout
| ||
contextmenu
|
Event | Data | Description |
---|---|---|
popupopen
| ||
popupclose
|
Event | Data | Description |
---|---|---|
tooltipopen
| ||
tooltipclose
|
Methods
Method | Returns | Description |
---|---|---|
toGeoJSON() |
Object |
Returns a |
getLatLngs() |
LatLng[] |
Returns an array of the points in the path, or nested arrays of points in case of multi-polyline. |
setLatLngs( |
this |
Replaces all the points in the polyline with the given array of geographical points. |
isEmpty() |
Boolean |
Returns |
getCenter() |
LatLng |
Returns the center (centroid) of the polyline. |
getBounds() |
LatLngBounds |
Returns the |
addLatLng( |
this |
Adds a given point to the polyline. By default, adds to the first ring of
the polyline in case of a multi-polyline, but can be overridden by passing
a specific ring as a LatLng array (that you can earlier access with |
Method | Returns | Description |
---|---|---|
redraw() |
this |
Redraws the layer. Sometimes useful after you changed the coordinates that the path uses. |
setStyle( |
this |
Changes the appearance of a Path based on the options in the |
bringToFront() |
this |
Brings the layer to the top of all path layers. |
bringToBack() |
this |
Brings the layer to the bottom of all path layers. |
Method | Returns | Description |
---|---|---|
bindPopup( |
this |
Binds a popup to the layer with the passed |
unbindPopup() |
this |
Removes the popup previously bound with |
openPopup( |
this |
Opens the bound popup at the specificed |
closePopup() |
this |
Closes the popup bound to this layer if it is open. |
togglePopup() |
this |
Opens or closes the popup bound to this layer depending on its current state. |
isPopupOpen() |
boolean |
Returns |
setPopupContent( |
this |
Sets the content of the popup bound to this layer. |
getPopup() |
Popup |
Returns the popup bound to this layer. |
Method | Returns | Description |
---|---|---|
bindTooltip( |
this |
Binds a tooltip to the layer with the passed |
unbindTooltip() |
this |
Removes the tooltip previously bound with |
openTooltip( |
this |
Opens the bound tooltip at the specificed |
closeTooltip() |
this |
Closes the tooltip bound to this layer if it is open. |
toggleTooltip() |
this |
Opens or closes the tooltip bound to this layer depending on its current state. |
isTooltipOpen() |
boolean |
Returns |
setTooltipContent( |
this |
Sets the content of the tooltip bound to this layer. |
getTooltip() |
Tooltip |
Returns the tooltip bound to this layer. |
Method | Returns | Description |
---|---|---|
addTo( |
this |
Adds the layer to the given map |
remove() |
this |
Removes the layer from the map it is currently active on. |
removeFrom( |
this |
Removes the layer from the given map |
getPane( |
HTMLElement |
Returns the |
Method | Returns | Description |
---|---|---|
on( |
this |
Adds a listener function ( |
on( |
this |
Adds a set of type/listener pairs, e.g. |
off( |
this |
Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to |
off( |
this |
Removes a set of type/listener pairs. |
off() |
this |
Removes all listeners to all events on the object. |
fire( |
this |
Fires an event of the specified type. You can optionally provide an data object — the first argument of the listener function will contain its properties. The event might can optionally be propagated to event parents. |
listens( |
Boolean |
Returns |
once(…) |
this |
Behaves as |
addEventParent( |
this |
Adds an event parent - an |
removeEventParent( |
this |
Removes an event parent, so it will stop receiving propagated events |
addEventListener(…) |
this |
Alias to |
removeEventListener(…) |
this |
Alias to |
clearAllEventListeners(…) |
this |
Alias to |
addOneTimeEventListener(…) |
this |
Alias to |
fireEvent(…) |
this |
Alias to |
hasEventListeners(…) |
Boolean |
Alias to |