【转载】Leaflet中⽂api L.Map
API各种类中的核⼼部分,⽤来在页⾯中创建地图并操纵地图.
使⽤ example
// initialize the map on the "map" div with a given center and zoom
var map = L.map('map', {
center: [51.505, -0.09],
zoom: 13
});
构造器
构造器使⽤描述
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.
maxBounds null 当这个选项被设置后,地图被限制在给定的地理边界内,当⽤户平移将地图拖动到视图以外的范围时会出现弹回的效果,并且也不允许缩⼩视图到给定范围以外的区域(这取决于地图的
尺⼨)。使⽤⽅法可以动态地设置这种约束.
crs L.CRS.
EPSG3857
使⽤的坐标系,当你不确定坐标系是什么时请不要更改. Interaction Options
选项类型默认
描述
dragging Boolean true决定地图是否可被⿏标或触摸拖动.
touchZoom Boolean true决定地图是否可被两只⼿指触摸拖拽缩放.
scrollWheelZoom Boolean true决定地图是否被被⿏标滚轮滚动缩放.
doubleClickZoom Boolean true决定地图是否可被双击缩放.
boxZoom Boolean true决定地图是否可被缩放到⿏标拖拽出的矩形的视图,⿏标拖拽时需要同时按住shift键.
tap Boolean true Enables mobile hacks for supporting instant taps (fixing 200ms click delay on iOS/Android) and touch holds (fired as contextmenu events).
tapTolerance Number15The max number of pixels a user can shift his finger during touch for it to be considered a valid tap. trackResize Boolean true确定地图在窗⼝尺⼨改变时是否可以⾃动处理浏览器以更新视图.
worldCopyJump Boolean false 当这个选项可⽤时,当你平移地图到其另⼀个领域时会被地图捕获到,并⽆缝地跳转到原始的领域以保证所有标注、⽮量图层之类的覆盖物仍然可见.
closePopupOnClick Boolean 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 Boolean depends确定⽡⽚淡出动画是否可⽤。通常默认在所有浏览器中都⽀持CSS3转场,android例外. zoomAnimation Boolean depends确定⽡⽚缩放动画是否可⽤。通常默认在所有浏览器中都⽀持CSS3转场,android例外. zoomAnimationThreshold Number4Won't animate zoom if the zoom difference exceeds this value.
markerZoomAnimation Boolean 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⽅法)
地图状态修改
⽅法返
mousemove是什么键描述
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 Stops watching location previously initiated by map.locate({watch: true}) and aborts resetting the map view if map.locate was called with {setView: true}.
remove()this Destroys the map and clears all related event listeners.获取地图状态
⽅法返回
描述
getCenter()返回地图视图的地理中⼼. getZoom()Number获取地图视图现在所处的缩放级别. getMinZoom()Number返回地图最⼩的缩放级别. getMaxZoom()Number返回地图最⼤的缩放级别. getBounds()返回地图视图的经纬度边界.
getBoundsZoom( <> bounds, <Boolean> inside? )Number
返回适应整个地图视图边界的最⼤缩放级别。如果inside的设置时true,这个⽅法返回适应整个地图视图边界的最⼩缩放级别.
getSize()返回现有地图容器的⼤⼩.
getPixelBounds()Bounds返回地图视图在像素投影坐标系下的边界。(很多时候对⽤户⾃定义图层和叠加很有⽤). 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 Creates a popup with the specified options and opens it in the given point on a map. closePopup( <> popup? )this关闭打开的弹出框.
addControl( <> control )this在地图上添加控制选项.
removeControl( <> control )this在地图上移除控制选项.
转换⽅法
⽅法返回值描述
latLngToLayerPoint( <> latlng )返回地图图层上与地理坐标相⼀致的点。(在地图上进⾏位置叠加时⽐较有⽤). layerPointToLatLng( <> point )返回给定地图上点的地理坐标系.
containerPointToLayerPoint( <> point )将于地图容器相关的点转换为地图图层相关的点. layerPointToContainerPoint( <> point )将地图图层相关的点转换为地图容器相关的点.
latLngToContainerPoint( <> latlng )返回与给定地理坐标系相符的地图容器的点.
containerPointToLatLng( <> point )返回给定地理容器点的地理坐标.
project( <> latlng, <Number> zoom? )将地理坐标投影到指定缩放级别的像素坐标系中.
unproject( <> point, <Number> zoom? )将像素坐标系投影到指定缩放级别的地理坐标系中。(默认为当前的缩放级别). mouseEventToContainerPoint( <MouseEvent> event )返回⿏标点击事件对象的像素坐标(与地图左上⾓相关). mouseEventToLayerPoint( <MouseEvent> event )返回⿏标点击事件对象的像素坐标(与地图图层相关). mouseEventToLatLng( <MouseEvent> event )返回⿏标点击事件对象的地理坐标.
其他⽅法
⽅法返回值描述
getContainer()HTMLElement返回地图容器对象.
getPanes()返回不同地图对象的边框(叠加渲染).
whenReady( <Function> fn, <Object> context? )this当地图的位置和缩放初始化好或是时间发⽣之后,运⾏给定的回调⽅法,通常传递⼀个函数内容.
位置选项
选项类型默认
描述
watch Boolean false 如果该值为真,则开始利⽤W3C的watchPosition⽅法监听位置变化情况(⽽不是指监听⼀次)。你可以通过map.stoplocate()⽅法来停⽌监听.
setView Boolean false 如果该值为真,则通过⾃动将地图视图定位到⽤户⼀定精度范围内的位置,如果地理定位失败则显⽰全部地图.
maxZoom Number Infinity在使⽤setView选项时视图缩放的最⼤级别.
timeout Number10000在触发locationerror事件之前等待地理定位的毫秒为单位的时间.
maximumAge Number0位置监听的最⼤⽣命周期。如果⽐最后定位回复后毫秒⽤时短,则locate返回⼀个缓存位置. enableHighAccuracy Boolean false开启⾼精度,参加 .
Zoom/pan options
选项类型默认值描述
reset Boolean false If true, the map view will be completely reset (without any animations).
pan-Sets the options for the panning (without the zoom change) if it occurs.
zoom-Sets the options for the zoom change if it occurs.
animate Boolean-An equivalent of passing animate to both zoom and pan options (see below).
Pan options
选项类型默
描述
animate Boolean-If true, panning will always be animated if possible. If false, it will not animate panning, either resetting the map view if panning more than a screen away, or just setting a new offset for the map pane (except for `panBy` which always does the latter).
duration Number0.25Duration of animated panning.
easeLinearity Number0.25The curvature factor of panning animation easing (third parameter of the ). 1.0 means linear animation, the less the more bowed the curve.
noMoveStart Boolean false If true, panning won't fire movestart event on start (used internally for panning inertia). Zoom options
选项类型默
描述
animate Boolean-If not specified, zoom animation will happen if the zoom origin is inside the current view. If true, the map will attempt animating zoom disregarding where zoom origin is. Setting false will make it always reset the view completely without animation.
fitBounds options
The same as  and additionally:
选项类
描述
paddingTopLeft[0,0]Sets the amount of padding in the top left corner of a map container that shouldn'
t be accounted for when setting the view to fit bounds. Useful if you have some control overlays on the map like a sidebar and you don't want them to obscure objects you're zooming to.
paddingBottomRight[0,0]The same for bottom right corner of the map.