mapgrid.js
10.0 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//百度地图网格类
function SquareOverlay(center, size, name, data, type = 'admin', is_select = 0)
{
this._id = data.id; // 地图位置对应的数据库id
this._village_id = data.village_id; // 地图位置对应的数据库id
this._group_id = data.group_id; // 地图位置对应的数据库id
this._center = center; //地图经纬度
this._size = size; //覆盖物大小
this._name = name; //覆盖物名称
this._content = data; //覆盖物内容信息
this._over = data.alarmStatus ?? 0; //报警级别
this._type = type; // 地图类型
this._is_select = is_select; // 地图类型
}
SquareOverlay.prototype = new BMap.Overlay();
SquareOverlay.prototype.initialize = function (map) {
// 保存map对象实例
this._map = map;
// 创建div元素,作为自定义覆盖物的容器
let div = document.createElement("div");
div.style.position = "absolute";
div.className = "map-tip";
let image = document.createElement("img")
let span = document.createElement("span");
if (this._is_select == 1) {
image.src = "/assets/admin/img/point-selected.png";
image.style.width = 1 * this._size + "px";
} else {
image.src = "/assets/admin/img/point" + this._over + ".png";
image.style.width = this._size + "px";
}
image.style.display = "inline-block";
let color = getLevel(this._over);
let html = "<a onclick='getMapInfoForSpan("+this._id+",0,0,\""+this._type+"\",\""+this._name+"\")' data-id=this._id><span map-id=" + this._content.id + " class='item' style='color:" + color + "'>" + this._name + "</span></a>";
span.innerHTML = html;
console.log("_is_select == " + this._is_select)
if (this._is_select == 1) {
span.style.display = "inline-block";
span.style.fontWeight = "normal";
span.style.padding = "8px 12px";
span.style.borderRadius = "3px";
span.style.backgroundColor = "rgba(255,255,255,0.8)";
div.style.zIndex = 1000;
div.style.width = "3000px";
} else {
span.style.display = "none";
}
span.style.position = "absolute";
span.style.fontSize = "16px";
span.style.color = "#fff";
image.style.cursor = "pointer";
//span.setAttribute("content", JSON.stringify(this._content));
//image.setAttribute("content", JSON.stringify(this._content));
div.setAttribute("content", JSON.stringify(this._content));
div.appendChild(image);
div.appendChild(span);
//div.style.width = "fit-content";
div.style.zIndex = 100 - this._isOver;
this._image = image;
//console.log(this._image);
this._span = span;
// 可以根据参数设置元素外观
// 将div添加到覆盖物容器中
map.getPanes().markerPane.appendChild(div);
// 保存div实例
this._div = div;
// 需要将div元素作为方法的返回值,当调用该覆盖物的show、
// hide方法,或者对覆盖物进行移除时,API都将操作此元素。
return div;
}
SquareOverlay.prototype.draw = function () {
// 根据地理坐标转换为像素坐标,并设置给容器
var position = this._map.pointToOverlayPixel(this._center);
this._div.style.left = position.x - this._size / 2 + "px";
this._div.style.top = position.y - this._size + "px";
}
SquareOverlay.prototype.show = function () {
if (this._div) {
this._div.style.display = "";
}
}
// 实现隐藏方法
SquareOverlay.prototype.hide = function () {
if (this._div) {
this._div.style.display = "none";
}
}
SquareOverlay.prototype.toggle = function () {
if (this._div) {
if (this._div.style.display == "") {
this.hide();
} else {
this.show();
}
}
};
//添加到百度地图
SquareOverlay.prototype.addMap = function (map) {
map.addOverlay(this);
}
// 标注大小改变
SquareOverlay.prototype.changeSize = function (zoom) {
// console.info(zoom);
this._image.style.width = this._size + (zoom - 15) * 4 + "px";
//this._image.style.position = "absolute";
//this._image.style.bottom = "0";
this._span.style.fontSize = 20 + (zoom - 14) * 4 + "px";
}
//添加监听事件
SquareOverlay.prototype.addEventListener = function (el, event, func) {
this[el]['on' + event] = func;
}
SquareOverlay.prototype.getContent = function () {
return this._content;
}
SquareOverlay.prototype.changeBig = function () {
this._image.src = "/assets/admin/img/point-selected.png?v=1.202105";
this._image.style.width = 1 * this._size + "px";
this._span.style.display = "inline-block";
this._span.style.fontWeight = "normal";
this._span.style.padding = "8px 12px";
this._span.style.borderRadius = "3px";
this._span.style.backgroundColor = "rgba(255,255,255,0.8)";
this._div.style.zIndex = 1000;
this._div.style.width = "3000px";
}
SquareOverlay.prototype.changeSmall = function () {
this._image.src = "/assets/admin/img/point" + this._over + ".png";
this._image.style.width = 1 * this._size + "px";
this._span.style.display = "none";
this._div.style.zIndex = 10;
this._div.style.width = this._size + "px";
}
//获取元素属性content
var getContent = function (_this) {
return JSON.parse(_this.getAttribute("content"));
}
//根据值获取颜色
/*不同的数值代表不同的颜色
*绿色(#00ff00) v = 0
*蓝色(#0000ff)0 < v <= 40
*黄色(#ffff00)40 < v <= 80
*橙色(#ffaa00)80 < v <= 120
*紫色(#ff00ff)120 < v <= 160
*红色(#ff0000)160 < v <= 200
*深红(#8b0000)200 < v
*/
var getColor = function (num) {
var colors = ["#00FF00", "#0000ff", "#ffff00", "#ffaa00", "#ff00ff", "#ff0000", "#8b0000"]
var color;
if (num == 0) {
color = colors[0];
} else if (num <= 40) {
color = colors[1];
} else if (num <= 80) {
color = colors[2];
} else if (num <= 120) {
color = colors[3];
} else if (num <= 160) {
color = colors[4];
} else if (num <= 200) {
color = colors[5];
} else {
color = colors[6];
}
return color;
}
/**
* 根据值获取红黄绿牌
* 2 红
* 1 黄
* 0 绿
* */
function getLevel(l)
{
let text = "#3399FF";
switch (l) {
case "4":
text = "red";
break;
case "3":
text = "#FF4D4D";
break;
case "2":
text = "#FFA54D";
break
case "1":
text = "#FFD1A5";
break
}
return text;
}
// 获取地图信息
// 弹窗展示房间的详细信息
function getMapInfoForSpan(id,longitude,latitude, type, name)
{
if (type === 'web') {
$("#alertTitle").text(name);
// 获取数据
$.ajax(
{
url: '/statistics/building-data?id='+id,
dataType : "json",
success: function (data) {
$("#alertBox_smoke").text(data.smoke_count);
$("#alertBox_smoke_abnormal").text(data.smoke_abnormal_count);
$("#alertBox_lock").text(data.lock_count);
$("#alertBox_lock_abnormal").text(data.lock_abnormal_count);
$("#alertBox").show();
},
error: function (data) {
}
}
);
//
} else {
layer_iframe('/admin/map/room?room_id=' + id,'房间',2,0.8,['1200px', '600px'], true)
return false;
setMarket(id);
const point = new BMap.Point(longitude,latitude);
// const marker = new BMap.Marker(point); // 创建标注
// map.addOverlay(marker); // 将标注添加到地图中
map.panTo(point); //转到该点位置
return;
// const point = new BMap.Point(longitude,latitude);
// var icon = new BMap.Icon('/assets/admin/img/point-selected.png', new BMap.Size(20, 32), {
// anchor: new BMap.Size(10, 30)
// });
// // map.clearOverlays(longitude,latitude);
// var new_point = new BMap.Point(longitude,latitude);
// var marker = new BMap.Marker(new_point); // 创建标注
// map.addOverlay(marker); // 将标注添加到地图中
// map.panTo(new_point); //转到该点位置
return;
//创建一个图像标注实例。point参数指定了图像标注所在的地理位置
var mkr = new BMap.Marker(new BMap.Point(longitude,latitude), {
icon: icon
});
//将覆盖物添加到地图中
map.addOverlay(mkr);
map.panTo(point); //转到该点位置
return;
// 创建div元素,作为自定义覆盖物的容器
let div = document.createElement("div");
div.style.position = "absolute";
div.className = "map-tip";
let image = document.createElement("img")
let span = document.createElement("span");
image.src = "/assets/admin/img/point-selected.png";
image.style.display = "inline-block";
image.style.width = "60px";
let color = getLevel(4);
let html = "<a onclick='getMapInfoForSpan("+id+")' data-id=this._id><span map-id=" + id + " class='item' style='color:" + color + "'>" + name + "</span></a>";
span.innerHTML = html;
span.style.display = "none";
span.style.position = "absolute";
span.style.fontSize = "16px";
span.style.color = "#fff";
image.style.cursor = "pointer";
//span.setAttribute("content", JSON.stringify(this._content));
//image.setAttribute("content", JSON.stringify(this._content));
div.setAttribute("content", JSON.stringify('sw'));
div.appendChild(image);
div.appendChild(span);
//div.style.width = "fit-content";
div.style.zIndex = 100;
//console.log(this._image);
// 可以根据参数设置元素外观
// 将div添加到覆盖物容器中
map.getPanes().markerPane.appendChild(div);
// point.getPanelRect.
map.panTo(point); //转到该点位置
// layer_iframe('/admin/map/room?room_id=' + id,'房间',2,0.8,['1200px', '600px'], true)
}
}