map.js
7.4 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
var gb_real_gps_map = (function () {
var wrap = '#real_gps_data_page';
var map;
var init = function (lineCode, upDown, stations) {
//初始化地图
var point = new BMap.Point(121.544336, 31.221315)
, zoom = 18;
map = new BMap.Map($('.map_cont:eq(0)', wrap)[0]);
map.centerAndZoom(point, zoom);
map.enableScrollWheelZoom(true);
if (stations)
drawLine(lineCode, upDown, stations);
}
var drawLine = function (lineCode, upDown, data) {
map.clearOverlays();
gps_marker_arr = [];
var arr = data.stations[upDown];
drawStations(arr);//绘制站点
drawSections(arr);//绘制站点间路段
//定位到起点
map.setCenter(
new BMap.Point(arr[0].bd_lon, arr[0].bd_lat));
};
function drawStations(arr) {
var s, point, marker, w, iw, icon, text;
for (var i = 0, len = arr.length; i < len; i++) {
s = arr[i];
s.index = i + 1;
transCoord(s);
point = new BMap.Point(s.bd_lon, s.bd_lat);
marker = new BMap.Marker(point);
//根据站点名称 计算marker 宽度
w = s.name.length * 12 + 38;
iw = w - 2;
s._name = s.name;
icon = new BMap.Icon(gb_map_utils.createStationIcon(s, w)
, new BMap.Size(iw, 24), {anchor: new BMap.Size(iw / 2, 25)})
marker.setIcon(icon);
marker._data = s;
map.addOverlay(marker);
}
}
function drawSections(arr) {
var s, coords, pos, polyline;
var color = arr[0].upDown == 0 ? 'blue' : 'red'
, style = {strokeWeight: 5, strokeColor: color, strokeOpacity: .7};
for (var i = 0, len = arr.length; i < len; i++) {
s = arr[i];
pos = [];
$.each(s.paths, function () {
coords = TransGPS.wgsToBD(this.lat, this.lon);
pos.push(new BMap.Point(coords.lng, coords.lat));
});
polyline = new BMap.Polyline(pos, style);
map.addOverlay(polyline);
}
}
function transCoord(obj) {
if (obj.bd_lat)
return;
var coord = TransGPS.wgsToBD(obj.lat, obj.lon);
obj.bd_lat = coord.lat;
obj.bd_lon = coord.lng;
}
var gps_marker_arr = {}, topMarker;
var renderGps = function (arr) {
var devices = [];
var gps, m, w;
for (var i = 0, len = arr.length; i < len; i++) {
gps = arr[i];
//转换坐标
transCoord(gps);
m = gps_marker_arr[gps.deviceId];
if (!m) {
m = createGpsMarker(gps);
map.addOverlay(m);
gps_marker_arr[gps.deviceId] = m;
}
else if (gps.timestamp != m._data.timestamp) {
m.setPosition(new BMap.Point(gps.bd_lon, gps.bd_lat));
w = m._icon_width;
m.setIcon(new BMap.Icon(createCarIconRotation(gps, w), new BMap.Size(w, 70)));
}
m._data = gps;
if (m.infoWindow.isOpen())
bdOpenWindow(m);
devices.push(gps.deviceId);
}
//删除多余的Marker (改变走向的)
for (var k in gps_marker_arr) {
if (devices.indexOf(k) == -1) {
map.removeOverlay(gps_marker_arr[k]);
gps_marker_arr[k] = null;
delete gps_marker_arr[k];
}
}
};
var createGpsMarker = function (gps) {
var point = new BMap.Point(gps.bd_lon, gps.bd_lat)
, m = new BMap.Marker(point);
//根据编码长度 计算marker 宽度
var w = gps.nbbm.length * 8.85;
m._icon_width = w;
//渲染icon
m.setIcon(new BMap.Icon(createCarIconRotation(gps, w), new BMap.Size(w, 70)));
m.setTop(true);
m._data = gps;
m.infoWindow = new BMap.InfoWindow();
//click
m.addEventListener('click', function () {
bdOpenWindow(this);
});
//mouseover
m.addEventListener('mouseover', function () {
setTop(this);
});
return m;
};
function setTop(m) {
if (topMarker)
topMarker.setTop(false);
m.setTop(true);
topMarker = m;
}
function bdOpenWindow(marker) {
var gps = marker._data;
marker.infoWindow.setContent(template('gps_map_info_win-temp', gps));
map.openInfoWindow(marker.infoWindow, marker.point);
}
function createCarIconRotation(gps, w) {
if (!w)
w = 70;
var canvas = $('<canvas></canvas>')[0];
canvas.width = w;
canvas.height = 64;
var ctx = canvas.getContext('2d');
var bg_color = gps.upDown == 0 ? 'rgba(94, 150, 210, 1)' : 'rgba(201, 33, 33, 1)';
ctx.roundRect(0, 0, w, 19, 4).stroke();
ctx.fillStyle = bg_color;
ctx.fill();
//文字
ctx.font = "14px arial";
ctx.fillStyle = "#fff";
ctx.fillText(gps.nbbm, 2, 14);
//角度图标
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.rotate(gps.direction * Math.PI / 180);
/**
* 绘制角度图标
*/
ctx.beginPath();
ctx.strokeStyle = bg_color;
var circle = {
x: 0, //圆心的x轴坐标值
y: 0, //圆心的y轴坐标值
r: 12 //圆的半径
};
var d = 12.5;
//绘制圆形
ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, true);
//按照指定的路径绘制弧线
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.fill();
//下方矩形
ctx.beginPath();
ctx.fillStyle = bg_color;
ctx.fillRect(5 - d, 16 - d, 15, 4);
ctx.rect(5 - d, 11 - d, 7.5, 8);
ctx.rect(12.5 - d, 11 - d, 7.5, 8);
ctx.strokeStyle = bg_color;
ctx.stroke();
//上方三角形
ctx.beginPath();
ctx.fillStyle = "#111111";
ctx.moveTo(12.5 - d, 3 - d);
ctx.lineTo(6.5 - d, 7.5 - d);
ctx.lineTo(18.5 - d, 7.5 - d);
ctx.closePath();
ctx.fill();
ctx.restore();
return canvas.toDataURL();
}
//Canvas 带圆角的矩形
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
if (w < 2 * r) r = w / 2;
if (h < 2 * r) r = h / 2;
this.strokeStyle = 'rgba(0,102,0,.1)';
this.beginPath();
this.moveTo(x + r, y);
this.arcTo(x + w, y, x + w, y + h, r);
this.arcTo(x + w, y + h, x, y + h, r);
this.arcTo(x, y + h, x, y, r);
this.arcTo(x, y, x + w, y, r);
this.closePath();
return this;
};
/**
* 定位到gps
* @param device
*/
var focus = function (device) {
var m = gps_marker_arr[device];
if (m) {
map.setCenter(m.getPosition());
bdOpenWindow(m);
}
}
return {
init: init,
drawLine: drawLine,
renderGps: renderGps,
focus: focus
}
})();