【Leafletjs】2.添加marker到地图

 本人建了一个Leaflet交流群:Leaflet&WebGIS  331437754

接着上篇我们在地图中添加一个marker,非常简单只需添加如下代码即可:

var marker = L.marker([30, 118])
.addTo(map);

Demo:http://jsfiddle.net/shitao1988/KDr4B/1/

给这个marker添加一个弹出框

var marker = L.marker([30, 118])
.addTo(map)
.bindPopup("<b>中国</b><br>安徽黄山.")
.openPopup();

结果如下:

设置Marker相关属性项:

{draggable: true, // 使图标可拖拽
title: 'Text', // 添加一个标题
opacity: 0.5} // 设置透明度
)

Demo:http://jsfiddle.net/shitao1988/KDr4B/2/

下面是marker相关的插件:

1. Leaflet.awesome-markers:提供了丰富多彩的图标

Demo:http://jsfiddle.net/VPzu4/92/

2.Leaflet.markercluster:强大的集聚插件

 

3.Leaflet.label:美化的label

4.Leaflet.AnimatedMarker:使marker沿线运动

5.leaflet.bouncemarker:加载marker时有个弹跳动画

6.OverlappingMarkerSpiderfier-Leaflet:处理重叠在一起的markers

7.Leaflet.EdgeMarker:在边框上显示不在当前视野中的marker

 

Leaflet Marker API地址

http://leafletjs.com/reference.html#marker

Marker

用来在地图中放置注记。

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

Creation

FactoryDescription
L.marker( <LatLnglatlng, <Marker optionsoptions? ) 通过给定一个地理点和一个具有选项的对象来实例化一个注记。

Options

OptionTypeDefaultDescription
iconL.Icon*图标类用来表达注记。参加Icon documentation以了解自定义注记图标的详细信息。默认设置为new L.Icon.Default())。
clickableBooleantrue如果是false,注记则不产生鼠标事件并表现为底层地图的一部分。
draggableBooleanfalse确定注记是否可被鼠标或触摸拖动。
keyboardBooleantrueWhether the marker can be tabbed to with a keyboard and clicked by pressing enter.
titleString''注记旁边显示浏览器提示的文本信息。
altString''Text for the alt attribute of the icon image (useful for accessibility).
zIndexOffsetNumber0默认情况下,注记图片的叠置顺序由纬度自动设置。如果你想将某一注记放置于其他之上可用这个选项,设置一个较大的值即可,比如1000 (或是相反地设置一个较大的负值)。
opacityNumber1.0确定注记的不透明度。
riseOnHoverBooleanfalse如果此值为true,则当把鼠标放置于注记之上时,注记会显示与其他注记之上。
riseOffsetNumber250riseOnHover要素凸显时高度的补偿值。

事件/h3>

You can subscribe to the following events using these methods.

事件数据描述
clickMouseEvent当鼠标点击注记时触发。
dblclickMouseEvent当鼠标双击注记时触发。
mousedownMouseEvent当鼠标按下鼠标键时触发。.
mouseoverMouseEvent当鼠标在注记上时触发。.
mouseoutMouseEvent当鼠标离开注记时触发。
contextmenuMouseEvent当鼠标右击注记时触发。
dragstartEvent当用户拖动注记时触发。
dragEvent当用户拖动注记时不断触发。
dragendDragEndEvent当用户停止拖动注记时触发。
moveEvent当注记通过定义经纬度而移动时触发。新的坐标包含事件参数。
addEventFired when the marker is added to the map.
removeEvent当注记在地图上被删除时触发。
popupopenPopupEventFired when a popup bound to the marker is open.
popupclosePopupEventFired when a popup bound to the marker is closed.

Methods

方法返回值描述
addTo( <Mapmap )this在地图上添加注记。
getLatLng()LatLng返回当前注记的地理位置。
setLatLng( <LatLnglatlng )this将注记位置更改到给定点。
setIcon( <Iconicon )this更改注记的图标。
setZIndexOffset( <Number> offset )this更改注记的zIndex offset
setOpacity( <Number> opacity )this更改注记的透明度。
update()this更新注记的位置,在直接更改经纬度对象的坐标时比较有用。
bindPopup( <String> html | <HTMLElement> el | <Popuppopup, <Popup optionsoptions? )this当点击一个注记时绑定一个特定的HTML内容的弹出窗口。你也可以用Marker中的openPopup方法打开绑定的弹出窗口
unbindPopup()this将先前用bindPopup方法绑定的注记取消。
openPopup()this打开先前用 bindPopup 方法绑定的弹出框
getPopup()PopupReturns the popup previously bound by the bindPopup method.
closePopup()this关闭已打开的注记的弹出框。
togglePopup()thisToggles the popup previously bound by the bindPopup method.
setPopupContent( <String> html | <HTMLElement> el, <Popup optionsoptions? ) thisSets an HTML content of the popup of this marker.
toGeoJSON()ObjectReturns a GeoJSON representation of the marker (GeoJSON Point Feature).

Interaction handlers

Interaction handlers are properties of a marker instance that allow you to control interaction behavior in runtime, enabling or disabling certain features such as dragging (see IHandler methods). Example:

marker.dragging.disable();
PropertyTypeDescription
draggingIHandlerMarker dragging handler (by both mouse and touch).

 

转载于:https://www.cnblogs.com/shitao/p/3538246.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值