leafletApi中⽂⽂档L.Map
API各种类中的核⼼部分,⽤来在页⾯中创建地图并操纵地图.
使⽤ example
1. // initialize the map on the "map" div with a given center and zoom
2. var map = L.map('map', {
3. center: [51.505, -0.09],
4. zoom: 13
5. });
构造器
构造器使⽤描述
L.Map( <HTMLElement|String> id, <> options? )new L.Map(
…)
L.map(…)
通过div元素和带有地图选项的描述的⽂字对象来实例化⼀个地图对象,其中⽂字对象是可
选的。
Options
Map State Options
选项类型默认
描述
center null初始化地图的地理中⼼.
阿丘老婆zoom Number null初始化地图的缩放.
layers[]null初始化后加载到地图上的图层.
minZoom Number null地图的最⼩视图。可以重写地图图层的minZoom. maxZoom Number null地图的最⼤视图。可以重写地图图层的maxZoom.
maxBoun ds null
当这个选项被设置后,地图被限制在给定的地理边界内,当⽤户平移将地图拖动到视图以外的范围时会出现弹回的效果, 并且也不允许缩⼩视图到给定范围以外的区域(这取决于地图的尺⼨)。 使⽤⽅法可以动态地设置这种约束.
crs L.CR
S.
EPSG
3857
使⽤的坐标系,当你不确定坐标系是什么时请不要更改.
Interaction Options
选项类型默认
描述
dragging Boole
an
true决定地图是否可被⿏标或触摸拖动.
touchZoom Boole
an
true决定地图是否可被两只⼿指触摸拖拽缩放.
scrollWheelZo om Boole
an
true决定地图是否被被⿏标滚轮滚动缩放.
doubleClickZo om Boole
an
true决定地图是否可被双击缩放.
boxZoom Boole
an
true决定地图是否可被缩放到⿏标拖拽出的矩形的视图,⿏标拖拽时需要同时按住shift键.
tap Boole
an
true
Enables mobile hacks for supporting instant taps (fixing 200ms click delay on iOS/Android) and touch
holds (fired as contextmenu events).
tapTolerance Num
ber
15The max number of pixels a user can shift his finger during touch for it to be considered a valid tap.
trackResize Boole
an
true确定地图在窗⼝尺⼨改变时是否可以⾃动处理浏览器以更新视图.
worldCopyJu mp Boole
an
false
当这个选项可⽤时,当你平移地图到其另⼀个领域时会被地图捕获到,并⽆缝地跳转到原始的领域以保证所有标注、⽮量
图层之类的覆盖物仍然可见.
closePopupO nClick Boole
an
true当你不想⽤户点击地图关闭消息弹出框时,请将其设置为false .
Keyboard Navigation Options
选项类型默认值描述
keyboard Boolean true聚焦到地图且允许⽤户通过键盘的⽅向键和+/-键来漫游地图. keyboardPanOffset Number80确定按键盘⽅向键时地图平移的像素.
keyboardZoomOffset Number1确定键盘+ or -键对于的缩放级数.
Panning Inertia Options
选项类型默认值描述
inertia Boolean true如果该选项可⽤,在拖动和在某⼀时间段内持续朝同⼀⽅向移动建有动⼒的地图时,会有惯性的效果. inertiaDeceleration Number3000确定惯性移动减速的速率,单位是像素每秒的
⼆次⽅2.
inertiaMaxSpeed Number1500惯性移动的最⼤速度,单位是像素每秒.
inertiaThreshold Number depends放开⿏标或是触摸来停⽌惯性移动与移动停⽌之间的毫秒数.
Control options
选项类型默认值描述
zoomControl Boolean true确定是否默认加载在地图上 .
attributionControl Boolean true确定是否默认加载在地图上.
Animation options
选项类型默认值描述
fadeAnimation Boolea
n
depends确定⽡⽚淡出动画是否可⽤。通常默认在所有浏览器中都⽀持CSS3转场,android例外.
zoomAnimation Boolea
n
depends确定⽡⽚缩放动画是否可⽤。通常默认在所有浏览器中都⽀持CSS3转场,android例外.
zoomAnimationTh reshold Numbe
在水一方歌词r
4Won't animate zoom if the zoom difference exceeds this value.
markerZoomAnim ation Boolea
n
depends
确定注记的缩放是否随地图缩放动画⽽播放,如果被禁⽤,注记在动画中拉长时会消失。通常默认在所有浏览
器中都⽀持CSS3转场,android例外.
Events
You can subscribe to the following events using .
Event Data描述
click⽤户点击或触摸地图时触发.
dblclick⽤户双击或连续两次触摸地图时触发.
mousedown⽤户按下⿏标按键时触发.
mouseup⽤户按下⿏标按键时触发.
mouseover⿏标进⼊地图时触发.
mouseout⿏标离开地图时触发.
mousemove⿏标在地图上移动时触发.
contextmenu当⽤户在地图上按下⿏标右键时触发,如果有在监听这个时间,则浏览器默认的情景菜单被禁⽤.
focus当⽤户在地图上进⾏标引、点击或移动时进⾏聚焦.
blur当地图失去焦点时触发.
preclick当⿏标在地图上点击之前触发。有时会在点击⿏标时,并在已存在的点击事件开始处理之前想要某件事情发⽣时⽤得到.
load当地图初始化时触发。(当地图的中⼼点和缩放初次设置时).
unload Fired when the map is destroyed with  method.
viewreset当地图需要重绘内容时触发。(通常在地图缩放和载⼊时发⽣)这对于创建⽤户⾃定义的叠置图层⾮常有⽤. movestart地图视图开始改变时触发。(⽐如⽤户开始拖动地图).
move所有的地图视图移动时触发.
moveend当地图视图结束改变时触发。(⽐如⽤户停⽌拖动地图).
dragstart⽤户开始拖动地图时触发.
drag⽤户拖动地图时不断重复地触发.
dragend⽤户停⽌拖动时触发.
zoomstart当地图缩放即将发⽣时触发。(⽐如缩放动作开始前).
zoomend当地图缩放时触发.
zoomlevelschange Fired when the number of zoomlevels on the map is changed due to adding or removing a layer. resize Fired when the map is resized.
autopanstart打开弹出窗⼝时地图开始⾃动平移时触发.
layeradd当⼀个新的图层添加到地图上时触发.
layerremove当⼀些图层从地图上移除时触发.
baselayerchange当通过改变基础图层时触发.
overlayadd Fired when an overlay is selected through the .
overlayremove Fired when an overlay is deselected through the .
locationfound当地理寻址成功时触发(使⽤⽅法)一路向西演员
locationerror当地理寻址错误时触发(使⽤⽅法)
popupopen当弹出框打开时触发(使⽤openPopup⽅法)
popupclose当弹出框关闭时触发(使⽤closePopup⽅法)
地图状态修改
爱的火花⽅法返回值描述
setView( <> center, <Number> zoom,
<> options? )
this设定地图(设定其地理中⼼和缩放).
setZoom( <Number> zoom,
<> options? )
this设定地图的缩放.
zoomIn( <Number> delta?,
<> options? )
this通过delta变量放⼤地图的级别,1是delta的默认值.
zoomOut( <Number> delta?,
<> options?)
this通过delta变量缩⼩地图的级别,1是delta的默认值.
setZoomAround( <> latlng,
<Number> zoom, <> options? )this
Zooms the map while keeping a specified point on the map stationary (e.g. used internally for scroll zoom and double-click zoom).
fitBounds( <> bounds, <> options? )this将地图视图尽可能⼤地设定在给定的地理边界内.
fitWorld( <> options? )this将地图视图尽可能⼤地设定在包含全部地域的级别上.
panTo( <> latlng, <> options? )this将地图平移到给定的中⼼。如果新的中⼼点在屏幕内与现有的中⼼点不同则产⽣平移动作.
panInsideBounds( <> bounds )this平移地图到坐落于给定边界最接近的视图内. panBy( <> point, <> options? )this通过给定的像素值对地图进⾏平移.
invalidateSize( <Boolean> options?, <> options? )this
检查地图容器的⼤⼩是否改变并更新地图,如果是这样的话,在动态改变地图⼤⼩后调⽤,如果animate 是true的话,对地图进⾏更新.
setMaxBounds( <> bounds,
<> options? )
this将地图限定在给定的边界内 ().
locate( <> options? )this ⽤地理定位接⼝获取⽤户位置信息,在成功定位或定位出错产⽣locationerror后解除location-found事件与定位数据,且将地图视图设定到检测的确切的⽤户的位置(如果定位失败则回到地域视图)。在中有更多详细内容。
stopLocate()this 停⽌观看以前由map.locate({.:true})发起的位置,如果使⽤{setView:true}调⽤map.locate,则中⽌重置地图视图。
remove()this销毁地图并清除所有相关事件侦听器。获取地图状态
⽅法返回
辉く空の静寂には
描述
getCenter()返回地图视图的地理中⼼.
getZoom()Numb
er
获取地图视图现在所处的缩放级别.
getMinZoom()Numb
er
返回地图最⼩的缩放级别.
getMaxZoom()Numb
er
返回地图最⼤的缩放级别.
getBounds()返回地图视图的经纬度边界.
getBoundsZoom( <> bounds, <Boolean> inside? )Numb
er
返回适应整个地图视图边界的最⼤缩放级别。如果inside的设置时true,这个⽅法返回适应整个地图视图边界的最⼩缩放级别.
getSize()返回现有地图容器的⼤⼩.
getPixelBounds()Boun
ds
返回地图视图在像素投影坐标系下的边界。(很多时候对⽤户⾃定义图层和叠加很有⽤).
getPixelOrigin()返回地图图层像素投影坐标系下的左上⾓的点。(很多时候对⽤户⾃定义图层和叠加很有⽤).图层控制/h3>
⽅法返回值描述
addLayer( <> layer,
<Boolean> insertAtTheBottom? )this
将图层添加到地图上。如果insertAtTheBottom的选项为true,图层添加时在所以图层
之下。(在切换基底图时⽐较有⽤).
removeLayer( <> layer )this将图层在地图上移除.
hasLayer( <> layer )Boolean如果添加的图层是当前图层则返回true.
openPopup( <> popup )this当关闭前⼀个弹出框时弹出指定的对话框。(确定在同⼀时间只有⼀个打开并可⽤).
美丽的夜晚openPopup( <String> html | <HTMLElement> el,
<> latlng, <> options? )
this创建带有指定选项的弹出窗⼝,并在地图上的给定点打开弹出窗⼝。closePopup( <> popup? )this关闭打开的弹出框.
addControl( <> control )this在地图上添加控制选项.
removeControl( <> control )this在地图上移除控制选项.