Usage example 用法示例
// define rectangle geographical bounds
var bounds = [[54.559322, -5.767822], [56.1210604, -3.021240]];
// create an orange rectangle
L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(map);
// zoom the map to the rectangle bounds
map.fitBounds(bounds);
Creation 创作
Factory | Description |
---|---|
L.rectangle(<LatLngBounds> latLngBounds, <Polyline options> options?) |
Options
▶ Options inherited from Polyline 从折线继承的选项
▶ Options inherited from Path 从路径继承的选项
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.是否沿路径绘制描边。将其设置为 false 可禁用多边形或圆上的边框。 |
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.定义描边虚线图案的字符串。在某些旧浏览器中,不适用于由 Canvas 提供支持的图层。 |
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.一个字符串,用于定义到破折号模式中开始破折号的距离。在某些旧浏览器中,不适用于由 Canvas 提供支持的图层。 |
fill | Boolean | depends | Whether to fill the path with color. Set it to false to disable filling on polygons or circles.是否用颜色填充路径。将其设置为 false 可禁用多边形或圆的填充。 |
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. 定义如何确定形状内部的字符串。 |
bubblingMouseEvents | Boolean | true | When true , a mouse event on this path will trigger the same event on the map (unless L.DomEvent.stopPropagation is used).如果为 true,则此路径上的鼠标事件将在地图上触发相同的事件(除非使用 L.DomEvent.stop 传播)。 |
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. 在元素上设置的自定义类名。仅适用于 SVG 渲染器。 |
▶ Options inherited from Interactive layer
从交互式图层继承的选项
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.如果为 false,则该图层将不会发出鼠标事件,并将充当基础地图的一部分。 |
▶ Options inherited from Layer
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. 默认情况下,该图层将添加到地图的叠加窗格中。默认情况下,覆盖此选项将导致图层放置在另一个窗格上。 |
attribution | String | null | String to be shown in the attribution control, e.g. “© OpenStreetMap contributors”. It describes the layer data and is often a legal obligation towards copyright holders and tile providers. 要在归因控件中显示的字符串,例如“©开放街道地图贡献者”。它描述了图层数据,通常是对版权所有者和切片提供商的法律义务。 |
Events
▶ Mouse events inherited from Interactive layer
从交互式图层继承的鼠标事件
Event | Data | Description |
---|---|---|
click | MouseEvent | Fired when the user clicks (or taps) the layer. 当用户单击(或点击)图层时触发。 |
dblclick | MouseEvent | Fired when the user double-clicks (or double-taps) the layer. 当用户双击(或双击)图层时触发。 |
mousedown | MouseEvent | Fired when the user pushes the mouse button on the layer. 当用户按下图层上的鼠标按钮时触发。 |
mouseup | MouseEvent | Fired when the user releases the mouse button pushed on the layer. 当用户松开按下图层上的鼠标按钮时触发。 |
mouseover | MouseEvent | Fired when the mouse enters the layer. 鼠标进入图层时触发。 |
mouseout | MouseEvent | Fired when the mouse leaves the layer. 鼠标离开图层时触发。 |
contextmenu | MouseEvent | Fired when the user right-clicks on the layer, prevents default browser context menu from showing if there are listeners on this event. Also fired on mobile when the user holds a single touch for a second (also called long press). 当用户右键单击图层时触发,可防止默认浏览器上下文菜单显示此事件是否有侦听器。当用户按住一次触摸一秒钟(也称为长按)时,也会在移动设备上触发。 |
▶ Events inherited from Layer
从图层继承的事件
Event | Data | Description |
---|---|---|
add | Event | Fired after the layer is added to a map 将图层添加到地图后触发 |
remove | Event | Fired after the layer is removed from a map 从地图中移除图层后触发 |
▶ Popup events inherited from Layer
从图层继承的弹出事件
Event | Data | Description |
---|---|---|
popupopen | PopupEvent | Fired when a popup bound to this layer is opened 打开绑定到此图层的弹出窗口时触发 |
popupclose | PopupEvent | Fired when a popup bound to this layer is closed 当绑定到此图层的弹出窗口关闭时触发 |
▶ Tooltip events inherited from Layer
从图层继承的工具提示事件
Event | Data | Description |
---|---|---|
tooltipopen | TooltipEvent | Fired when a tooltip bound to this layer is opened. 打开绑定到此图层的工具提示时激发。 |
tooltipclose | TooltipEvent | Fired when a tooltip bound to this layer is closed. 当绑定到此层的工具提示关闭时激发。 |
Methods
Method | Returns | Description |
---|---|---|
setBounds(<LatLngBounds> latLngBounds) | this | Redraws the rectangle with the passed bounds. 重绘具有传递边界的矩形。 |
▶ Methods inherited from Polygon
从多边形继承的方法
Method | Returns | Description |
---|---|---|
toGeoJSON(<Number|false> precision?) | Object | Coordinates values are rounded with formatNum function with given precision . Returns a GeoJSON representation of the polygon (as a GeoJSON Polygon or MultiPolygon Feature).坐标值使用具有给定精度的 formatNum 函数进行舍入。返回面的地理 JSON 制图表达(作为 GeoJSON 面或多面要素)。 |
getCenter() | LatLng | Returns the center (centroid) of the Polygon. 返回多边形的中心(质心)。 |
▶ Methods inherited from Polyline
从折线继承的方法
Method | Returns | Description |
---|---|---|
getLatLngs() | LatLng[] | Returns an array of the points in the path, or nested arrays of points in case of multi-polyline. 返回路径中点的数组,或在多折线的情况下返回嵌套的点数组。 |
setLatLngs(<LatLng[]> latlngs) | this | Replaces all the points in the polyline with the given array of geographical points. 将折线中的所有点替换为给定的地理点数组。 |
isEmpty() | Boolean | Returns true if the Polyline has no LatLngs.如果折线没有纬度,则返回 true。 |
closestLayerPoint(<Point> p) | Point | Returns the point closest to p on the Polyline.返回折线上最接近 p 的点。 |
getBounds() | LatLngBounds | Returns the LatLngBounds of the path.返回路径的纬度边界。 |
addLatLng(<LatLng> latlng, <LatLng[]> latlngs?) | 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 getLatLngs ).将给定点添加到折线。默认情况下,在多折线的情况下,会添加到折线的第一个环,但可以通过将特定环作为 LatLng 数组传递来覆盖(您可以更早地使用 getLatLngs 访问该数组)。 |
▶ Methods inherited from Path
从路径继承的方法
Method | Returns | Description |
---|---|---|
redraw() | this | Redraws the layer. Sometimes useful after you changed the coordinates that the path uses. 重绘图层。有时,在更改路径使用的坐标后很有用。 |
setStyle(<Path options> style) | this | Changes the appearance of a Path based on the options in the Path options object.根据“路径选项”对象中的选项更改路径的外观。 |
bringToFront() | this | Brings the layer to the top of all path layers. 将图层置于所有路径图层的顶部。 |
bringToBack() | this | Brings the layer to the bottom of all path layers. 使图层位于所有路径图层的底部。 |
▶ Methods inherited from Layer
从层继承的方法
Method | Returns | Description |
---|---|---|
addTo(<Map|LayerGroup> map) | this | Adds the layer to the given map or layer group. 将图层添加到给定的地图或图层组。 |
remove() | this | Removes the layer from the map it is currently active on. 从图层当前处于活动状态的地图中移除该图层。 |
removeFrom(<Map> map) | this | Removes the layer from the given map 从给定地图中移除图层 |
removeFrom(<LayerGroup> group) | this | Removes the layer from the given LayerGroup 从给定图层组中删除图层 |
getPane(<String> name?) | HTMLElement | Returns the HTMLElement representing the named pane on the map. If name is omitted, returns the pane for this layer.返回表示地图上命名窗格的 HTML 元素。如果省略 name,则返回此图层的窗格。 |
getAttribution() | String | Used by the attribution control , returns the attribution option.由归因控件使用,返回归因选项。 |
▶ Popup methods inherited from Layer
从图层继承的弹出窗口方法
Method | Returns | Description |
---|---|---|
bindPopup(<String|HTMLElement|Function|Popup> content, <Popup options> options?) | this | Binds a popup to the layer with the passed content and sets up the necessary event listeners. If a Function is passed it will receive the layer as the first argument and should return a String or HTMLElement .将弹出窗口绑定到包含传递内容的层,并设置必要的事件侦听器。如果传递函数,它将接收层作为第一个参数,并应返回字符串或 HTML 元素。 |
unbindPopup() | this | Removes the popup previously bound with bindPopup .删除以前绑定了绑定的弹出窗口。 |
openPopup(<LatLng> latlng?) | this | Opens the bound popup at the specified latlng or at the default popup anchor if no latlng is passed.在指定的 latlng 处打开绑定的弹出窗口,如果未传递任何 latlng,则在默认的弹出锚点处打开绑定的弹出窗口。 |
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 true if the popup bound to this layer is currently open.如果绑定到此图层的弹出窗口当前处于打开状态,则返回 true。 |
setPopupContent(<String|HTMLElement|Popup> content) | this | Sets the content of the popup bound to this layer. 设置绑定到此图层的弹出窗口的内容。 |
getPopup() | Popup | Returns the popup bound to this layer. 返回绑定到此图层的弹出窗口。 |
▶ Tooltip methods inherited from Layer
从图层继承的工具提示方法
Method | Returns | Description |
---|---|---|
bindTooltip(<String|HTMLElement|Function|Tooltip> content, <Tooltip options> options?) | this | Binds a tooltip to the layer with the passed content and sets up the necessary event listeners. If a Function is passed it will receive the layer as the first argument and should return a String or HTMLElement .将工具提示绑定到包含传递内容的图层,并设置必要的事件侦听器。如果传递函数,它将接收层作为第一个参数,并应返回字符串或 HTML 元素。 |
unbindTooltip() | this | Removes the tooltip previously bound with bindTooltip .删除以前绑定了工具提示工具提示。 |
openTooltip(<LatLng> latlng?) | this | Opens the bound tooltip at the specified latlng or at the default tooltip anchor if no latlng is passed.在指定的 latlng 处打开绑定的工具提示,如果未传递任何 latlng,则在默认工具提示锚点处打开绑定的工具提示。 |
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 true if the tooltip bound to this layer is currently open.如果绑定到此图层的工具提示当前处于打开状态,则返回 true。 |
setTooltipContent(<String|HTMLElement|Tooltip> content) | this | Sets the content of the tooltip bound to this layer. 设置绑定到此图层的工具提示的内容。 |
getTooltip() | Tooltip | Returns the tooltip bound to this layer. 返回绑定到此图层的工具提示。 |
▶ Methods inherited from Evented
从事件继承的方法
Method | Returns | Description |
---|---|---|
on(<String> type, <Function> fn, <Object> context?) | this | Adds a listener function (fn ) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick' ).将侦听器函数 (fn) 添加到对象的特定事件类型。您可以选择指定侦听器的上下文(this 关键字将指向的对象)。您还可以传递几种空格分隔的类型(例如,“单击数据库单击”)。 |
on(<Object> eventMap) | this | Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove} 添加一组类型/侦听器对,例如 {单击:单击:单击,鼠标移动:鼠标移动} |
off(<String> type, <Function> fn?, <Object> context?) | 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 on , you must pass the same context to off in order to remove the listener.删除以前添加的侦听器函数。如果未指定任何函数,它将从对象中删除该特定事件的所有侦听器。请注意,如果将自定义上下文传递给 on,则必须将同一上下文传递给 off 才能删除侦听器。 |
off(<Object> eventMap) | this | Removes a set of type/listener pairs. 删除一组类型/侦听器对。 |
off() | this | Removes all listeners to all events on the object. This includes implicitly attached events. 删除对象上所有事件的所有侦听器。这包括隐式附加的事件。 |
fire(<String> type, <Object> data?, <Boolean> propagate?) | this | Fires an event of the specified type. You can optionally provide a data object — the first argument of the listener function will contain its properties. The event can optionally be propagated to event parents. 激发指定类型的事件。您可以选择提供一个数据对象 — 侦听器函数的第一个参数将包含其属性。可以选择将事件传播到事件父级。 |
listens(<String> type, <Boolean> propagate?) | Boolean | Returns true if a particular event type has any listeners attached to it. The verification can optionally be propagated, it will return true if parents have the listener attached to it.如果特定事件类型附加了任何侦听器,则返回 true。验证可以选择传播,如果父母附加了侦听器,它将返回 true。 |
once(…) | this | Behaves as on(…) , except the listener will only get fired once and then removed.行为如 on(…),只是侦听器只会被触发一次,然后被删除。 |
addEventParent(<Evented> obj) | this | Adds an event parent – an Evented that will receive propagated events添加事件父级 – 将接收传播事件的事件 |
removeEventParent(<Evented> obj) | this | Removes an event parent, so it will stop receiving propagated events 删除事件父级,使其停止接收传播的事件 |
addEventListener(…) | this | Alias to on(…) 别名到(…) |
removeEventListener(…) | this | Alias to off(…) |
clearAllEventListeners(…) | this | Alias to off() |
addOneTimeEventListener(…) | this | Alias to once(…) |
fireEvent(…) | this | Alias to fire(…) |
hasEventListeners(…) | Boolean | Alias to listens(…) |