forked from cjp314/BMP-fetch-point
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huiZhi.html
194 lines (173 loc) · 6.9 KB
/
huiZhi.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body,
html {
width: 100%;
height: 100%;
margin: 0;
font-family: "微软雅黑";
}
#allmap {
width: 100%;
height: 100%;
overflow: hidden;
}
#result {
width: 100%;
font-size: 12px;
}
dl,
dt,
dd,
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
p {
font-size: 12px;
}
dt {
font-size: 14px;
font-family: "微软雅黑";
font-weight: bold;
border-bottom: 1px dotted #000;
padding: 5px 0 5px 5px;
margin: 5px 0;
}
dd {
padding: 5px 0 0 5px;
}
li {
line-height: 28px;
}
.btn {
color: red;
}
.anchorBL {
display: none
}
</style>
<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=E4805d16520de693a3fe707cdc962045"></script>
<!--加载鼠标绘制工具-->
<script type="text/javascript"
src="//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<link rel="stylesheet" href="//api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
<!--加载检索信息窗口-->
<!-- <script type="text/javascript"
src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" /> -->
<title>鼠标绘制选点工具</title>
</head>
<body>
<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">
<div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">
</div>
</div>
<div id="result" style="overflow: hidden;position: absolute;z-index: 100;top:95%;">
<input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)" class="btn" />
<input type="button" value="清除所有覆盖物" onclick="clearAll()" class="btn" />
<input type="button" value="获取绘制区域点集" onclick="getpoints()" class="btn" />
<div id="logParent" style="background-color: white;"></div>
</div>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map('map', { enableMapClick: false });
map.centerAndZoom(new BMap.Point(120.575588, 31.275412), 15); // 初始化地图,设置中心点坐标和地图级别
//map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
map.enableContinuousZoom(true); //启用地图惯性拖拽,默认禁用
map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT })); //向地图中添加比例尺控件
var overlays = [];
var overlaycomplete = function (e) {
var overlay = e.overlay;
overlays.push(overlay);
var markerMenu = new BMap.ContextMenu();
markerMenu.addItem(new BMap.MenuItem('移除', () => {
map.removeOverlay(overlay);
}));
overlay.addContextMenu(markerMenu);
};
var polygoncomplete = function (e, overlay) {
mylog(overlay.getPath())
};
var polylinecomplete = function (e, overlay) {
mylog(overlay.getPath())
};
var markercomplete = function (e, overlay) {
mylog(overlay.getPosition())
};
var styleOptions = {
strokeColor: "red", //边线颜色。
fillColor: "red", //填充颜色。当参数为空时,圆形将没有填充效果。
strokeWeight: 3, //边线的宽度,以像素为单位。
strokeOpacity: 0.8, //边线透明度,取值范围0 - 1。
fillOpacity: 0.6, //填充的透明度,取值范围0 - 1。
strokeStyle: 'solid' //边线的样式,solid或dashed。
}
//实例化鼠标绘制工具
var drawingManager = new BMapLib.DrawingManager(map, {
isOpen: false, //是否开启绘制模式
enableDrawingTool: true, //是否显示工具栏
drawingToolOptions: {
anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
offset: new BMap.Size(5, 5), //偏离值
},
circleOptions: styleOptions, //圆的样式
polylineOptions: styleOptions, //线的样式
polygonOptions: styleOptions, //多边形的样式
rectangleOptions: styleOptions //矩形的样式
});
//添加鼠标绘制工具监听事件,用于获取绘制结果
drawingManager.addEventListener('overlaycomplete', overlaycomplete);
drawingManager.addEventListener('polygoncomplete', polygoncomplete);
drawingManager.addEventListener('polylinecomplete', polylinecomplete);
drawingManager.addEventListener('markercomplete', markercomplete);
function clearAll() {
for (var i = 0; i < overlays.length; i++) {
map.removeOverlay(overlays[i]);
}
overlays.length = 0
logger.remove();
logger = document.createElement('div');
document.getElementById('logParent').appendChild(logger);
}
var logger = document.createElement('div');
document.getElementById('logParent').appendChild(logger);
function getpoints() {
console.log(overlays)
}
var pStyle = "color:green";
(function () {
mylog = function (message) {
var jsonMsg;
if (typeof message == 'object') {
jsonMsg = (JSON && JSON.stringify ? JSON.stringify(message) + "," : message);
} else {
jsonMsg = message;
}
var p = document.createElement('p');
if (pStyle == "color:green") {
pStyle = "color:blue";
} else {
pStyle = "color:green";
}
p.style = pStyle;
p.innerText = jsonMsg;
logger.appendChild(p);
}
})();
var size = new BMap.Size(80, 20);
map.addControl(new BMap.CityListControl({
anchor: BMAP_ANCHOR_TOP_LEFT,
offset: size,
}));
</script>
</body>
</html>