-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaode.html
68 lines (67 loc) · 2.36 KB
/
Gaode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>自定义事件</title>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
<style>
html,body,#container{
height:100%;
width:100%;
}
</style>
</head>
<body>
<div id="container" tabindex="0"></div>
<div class="input-card" style="width:18rem">
<h4>自定义事件的绑定与解绑</h4>
<div>
<div class="input-item">
<button id="bt1" class="btn" style="margin-right:1rem;">绑定事件</button>
<button id="bt2" class="btn">解绑事件</button>
</div>
</div>
</div>
<div class="info" id="tip">
给地图绑定自定义的count事件,实现新增marker点的计数<br>
<span id="count">总共添加了0个Marker.</span>
</div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
<script type="text/javascript">
var map = new AMap.Map('container', {
resizeEnable: true,
zoom: 10,
center: [116.39, 39.9]
});
var count = 0, clickListener;
var _onClick = function(e) {
new AMap.Marker({
position: e.lnglat,
map: map
})
map.emit('count', {count: count += 1});//触发自定义事件
}
var countDiv = document.getElementById('count');
var _onCount = function(e) {
countDiv.innerHTML = "总共添加了" + e.count + "个Marker.";
}
map.on("count", _onCount); //绑定自定义事件,返回监听对象
var bind = function() {
remove();//防止重复绑定
clickListener = AMap.event.addListener(map, "click", _onClick);//绑定地图事件
}
var remove = function() {
if (clickListener) {
AMap.event.removeListener(clickListener);//移除地图事件,以绑定时返回的对象作为参数
}
}
//绑定Dom事件
var button1 = document.getElementById('bt1');
var listener1 = AMap.event.addDomListener(button1, 'click', bind);//给div绑定单击事件
var button2 = document.getElementById('bt2');
var listener2 = AMap.event.addDomListener(button2, 'click', remove);
</script>
</body>
</html>