Marker 标记

hnsdk.Marker 用于在地图上显示可点击/可拖动的图标。扩展自 Layer

使用示例

hnsdk.marker([50.5, 30.5]).addTo(map);

Creation

构造函数说明
hnsdk.marker(latlng, options?)给出一个地理位置和可选的参数,实例化一个Marker对象。

Options 选项

选项类型默认值说明
iconIcon* 用于渲染标记(marker)的图标实例。有关如何自定义标记(marker)图标的详细信息,请参阅 Icon 文档。如果未指定,hnsdk.Icon.Default 则使用公共实例。
keyboardBooleantrue是否可以用键盘标记并按回车键进行点击。
titleString'' 悬停在标记(marker)上时出现的浏览器 tooltip 提示文本内容(默认情况下没有 tooltip 提示)。 对无障碍环境很有帮助
altString'Marker' Icon图标的 alt 属性提示文本内容。 对无障碍环境很有帮助
zIndexOffsetNumber0 默认情况下,标记(marker)图像 zIndex 是根据其纬度自动设置的。如果您想将标记置于所有其他标记(marker)之上(或之下),则使用此选项指定一个高值,如 1000(或高的负值)。
opacityNumber1.0标记(marker)的不透明度。
riseOnHoverBooleanfalse 如果为 true,当你把鼠标悬停在它上面时,该标记(marker)会在其他标记之上。
riseOffsetNumber250用于 riseOnHover 功能的 z-index 偏移。
paneString'markerPane'Map pane 将添加标记图标的位置。
shadowPaneString'shadowPane'Map pane 将添加标记阴影的位置。
bubblingMouseEventsBooleanfalse 当为 true 时,此标记上的鼠标事件将触发地图上的相同事件(除非使用 hnsdk.DomEvent.stopPropagation)。
autoPanOnFocusBooleantrue When true, the map will pan whenever the marker is focused (via e.g. pressing tab on the keyboard) to ensure the marker is visible within the map's bounds
事件数据说明
clickMouseEvent当用户 click(或 tap)图层时触发。
dblclickMouseEvent当用户 double-click(或 double-tap)图层时触发。
mousedownMouseEvent当用户在图层上按下鼠标按钮时触发。
mouseupMouseEvent当用户释放在图层上按下的鼠标按钮时触发。
mouseoverMouseEvent当鼠标进入图层时触发。
mouseoutMouseEvent当鼠标离开图层时触发。
contextmenuMouseEvent 当用户在图层上点击右键时触发,如果此事件有监听者,则阻止默认的浏览器上下文菜单显示。在移动设备上,当用户长按时,也会在手机上触发。
事件名数据说明
addEvent在图层添加到地图后触发
removeEvent从地图中移除图层后触发

Methods 方法

除了共享层方法,如 addTo()remove()、类似 bindPopup() 的 popup 方法之外,你还可以使用以下方法:
方法返回值说明
getLatLng()LatLng

返回标记的当前地理位置。

setLatLng(latlng)this

将标记位置更改为指定坐标。

setZIndexOffset(offset)this

更改标记的 zIndex 偏移量

getIcon()Icon

返回标记使用的当前图标

setIcon(icon)this

更改标记图标。

setOpacity(opacity)this

更改标记的透明度。

方法名返回值说明
on( type, fn, context?)this

为对象的特定事件类型添加一个监听函数(fn)。你可以选择性地指定监听器的上下文(这个关键字将指向的对象)。你也可以传递几个空格分隔的类型(例如,'click dblclick')。

on( eventMap)this

添加一组 type/listener,例如 {click: onClick, mousemove: onMouseMove}

off( type, fn?, context?)this

移除一个先前添加的监听器函数。如果没有指定函数,它将从对象中删除该特定事件的所有监听器。请注意,如果您向 on 传递了一个自定义的上下文,您必须向 off 传递相同的上下文,以便删除监听器。

off( eventMap)this

删除一组 type/listener

off()this

移除该对象上所有事件的所有监听器。这包括隐含的附加事件。

fire( type, data?, propagate?)this

触发指定类型的事件。您可以选择提供一个数据对象——侦听器函数的第一个参数将包含其属性,事件可以选择性地传播到事件父级。

listens( type, propagate?)Boolean

如果一个特定的事件类型有任何监听器连接到它,则返回 true。验证可以选择性地被传播,如果父级有监听器连接到它,它将返回true

once()this

on(...)的行为一样,不过监听器只会被触发一次后然后被删除。

addEventParent(obj)this

向父级 Evented 添加事件

removeEventParent(obj)this

删除之前向父级 Evented 添加的事件

addEventListener()this

on(…)

removeEventListener()this

off(…)

clearAllEventListeners()this

off()

addOneTimeEventListener()this

once(…)

fireEvent()this

fire(…)

hasEventListeners()Boolean

listens(…)

Last Updated:
Contributors: 小小志