Leaflet Map Control.Layers—图层控件使用户能够在不同的基础图层之间切换

Leaflet Map Control.Layers—图层控件使用户能够在不同的基础图层之间切换

The layers control gives users the ability to switch between different base layers and switch overlays on/off (check out the detailed example). Extends Control.

图层控件使用户能够在不同的基础图层之间切换,并打开/关闭覆盖(查看详细示例)。扩展控制。

Usage example

var baseLayers = {
    "Mapbox": mapbox,
    "OpenStreetMap": osm
};

var overlays = {
    "Marker": marker,
    "Roads": roadsLayer
};

L.control.layers(baseLayers, overlays).addTo(map);

The baseLayers and overlays parameters are object literals with layer names as keys and Layer objects as values:

baseLayers和overlay参数是对象文字,图层名称作为关键字,图层对象作为值:

{
    "<someName1>": layer1,
    "<someName2>": layer2
}

The layer names can contain HTML, which allows you to add additional styling to the items:

图层名称可以包含HTML,这允许您为项目添加其他样式:

{"<img src='my-layer-icon' /> <span class='my-layer-item'>My Layer</span>": myLayer}

Creation

FactoryDescription
L.control.layers(<Object> 
baselayers?, <Object> 
overlays?, <Control.Layers optionsoptions?)
Creates a layers control with the given layers. Base layers will be switched with radio buttons, while overlays will be switched with checkboxes. Note that all base layers should be passed in the base layers object, but only one should be added to the map during map instantiation.
使用给定图层创建图层控件。基本图层将通过单选按钮切换,而覆盖层将通过复选框切换。请注意,所有基本层都应在基本层对象中传递,但在映射实例化期间,只应将一个基本层添加到映射中。

Options

OptionTypeDefaultDescription
collapsedBooleantrueIf true, the control will be collapsed into an icon and expanded on mouse hover, touch, or keyboard activation.
如果为true,控件将折叠为图标,并在鼠标悬停、触摸或键盘激活时展开。
autoZIndexBooleantrueIf true, the control will assign zIndexes in increasing order to all of its layers so that the order is preserved when switching them on/off.
如果为true,控件将按递增顺序将zIndexes分配给其所有层,以便在打开/关闭它们时保留顺序。
hideSingleBaseBooleanfalseIf true, the base layers in the control will be hidden when there is only one.
如果为true,控件中的基础层将在只有一个时隐藏。
sortLayersBooleanfalseWhether to sort the layers. When false, layers will keep the order in which they were added to the control.
是否对图层进行排序。如果为false,图层将保持添加到控件的顺序。
sortFunctionFunction*compare function that will be used for sorting the layers, when sortLayers is true. The function receives both the L.Layer instances and their names, as in sortFunction(layerA, layerB, nameA, nameB). By default, it sorts layers alphabetically by their name.
当sortLayers为true时,将用于对层进行排序的比较函数。该函数同时接收L。层实例及其名称,如sortFunction(layerA,layerB,nameA,nameB)。默认情况下,它按图层名称的字母顺序对图层进行排序。

▶ Options inherited from Control

▶从控件继承的选项

OptionTypeDefaultDescription
positionString'topright'The position of the control (one of the map corners). Possible values are 'topleft''topright''bottomleft' or 'bottomright'
控件的位置(地图角之一)。可能的值为“topleft”、“topright”、“bottomleft”或“bottomright”

Methods

MethodReturnsDescription
addBaseLayer(<Layer
layer, <String> name)
thisAdds a base layer (radio button entry) with the given name to the control.
将具有给定名称的基本层(单选按钮条目)添加到控件中。
addOverlay(<Layer
layer, <String> name)
thisAdds an overlay (checkbox entry) with the given name to the control.
将具有给定名称的覆盖(复选框条目)添加到控件中。
removeLayer(<Layerlayer)thisRemove the given layer from the control.
从控件中删除给定层。
expand()thisExpand the control container if collapsed.
如果折叠,展开控件容器。
collapse()thisCollapse the control container if expanded.
如果展开,则折叠控件容器。

▶ Methods inherited from Control

从控件继承的方法

MethodReturnsDescription
getPosition()stringReturns the position of the control.
返回控件的位置。
setPosition(<string> 
position)
thisSets the position of the control.
设置控件的位置。
getContainer()HTMLElementReturns the HTMLElement that contains the control.
返回包含控件的HTMLElement。
addTo(<Mapmap)thisAdds the control to the given map.
将控件添加到给定的映射。
remove()thisRemoves the control from the map it is currently active on.
从当前处于活动状态的贴图中删除控件。
5 1 投票
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x