mapService.js
4.96 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
//import ClipboardJS from 'clipboard'
//window.ClipboardJS = ClipboardJS
//import EventBus from '../event-bus'
import { Message } from "element-ui";
// EventBus.$on('uiMap.copySuccess', (data)=>{
// Message({
// type: 'success',
// message: '已复制内容至粘贴板'
// });
// });
function MapService() {
this.map = null;
this.restainLayer = null;
}
MapService.prototype = {
// 设置默认图标
setDefaultIcon(marker) {
marker.setIcon(require('@/assets/images/icon_car.png'))
},
// 获取经纬度
getLngLat: function (lng, lat) {
return new AMap.LngLat(lng, lat);
},
// 清空地图覆盖物
clearMap: function (map) {
this.map.clearMap();
},
// 设置地图视角
setFitView: function (map) {
this.map.setFitView();
},
// 创建车辆位置点
createCarMarker: function (lnglatXY, map) {
var marker = new AMap.Marker({
position: lnglatXY,
icon: require('@/assets/images/icon_car.png'),
map: map,
autoRotation: true,
offset: new AMap.Pixel(-18, -11),
zIndex: 1020,
animation: 'AMAP_ANIMATION_DROP'
});
return marker;
},
// 创建位置点弹框
createActiveInfo(info) {
let msg = '报警'
// 激活
if (info.type == 1) {
msg = '激活'
}
// 定位
if (info.type == 2) {
msg = '定位'
}
return `<h3 class="map-infowindow-title">${info.name}/${info.standNo}</h3>
<div class="map-infowindow-ct">
<p><i class="icon icon-info_device"></i> 设备号: <span class="des">${info.sn} (${info.source == 1 ? '有线' : '无线'})</span></p>
<p><i class="icon icon-info_time"></i>${msg}时间: <span class="des"data-time>${info.time}</span></p>
<div style="display:${info.bsLng ? 'block' : 'none'}"><i class="icon icon-info_type"></i>定位类型: <a class="locate-btn active" id="locateTypeGps">GPS</a><a class="locate-btn" id="locateTypeBase">基站</a></div>
<p><i class="icon icon-info_location"></i> ${msg}地址: <span class="des" data-transferAddress>${info.address}</span></p>
</div>`
},
// 位置点弹框
bindActiveLocationInfoWindow(marker, map, info) {
var self = this
var infoDiv = document.createElement('div')
infoDiv.innerHTML = self.createActiveInfo(info)
// gps位置信息
function fillGPS() {
infoDiv.querySelector('[data-time]').textContent = info.time;
infoDiv.querySelector('[data-transferAddress]').textContent = info.address;
infoDiv.querySelector('#locateTypeGps').classList.add('active');
infoDiv.querySelector('#locateTypeBase').classList.remove('active');
marker.setPosition(self.getLngLat(info.lng, info.lat));
marker.infoWindow && marker.infoWindow.open(map, marker.getPosition());
}
// 基站位置信息
function fillBase() {
infoDiv.querySelector('[data-time]').textContent = info.bsTime;
infoDiv.querySelector('[data-transferAddress]').textContent = info.bsAddress;
infoDiv.querySelector('#locateTypeGps').classList.remove('active');
infoDiv.querySelector('#locateTypeBase').classList.add('active');
marker.setPosition(self.getLngLat(info.bsLng, info.bsLat));
marker.infoWindow && marker.infoWindow.open(map, marker.getPosition());
}
infoDiv.querySelector('#locateTypeGps').onclick = function (e) {
fillGPS()
}
infoDiv.querySelector('#locateTypeBase').onclick = function (e) {
fillBase()
}
if (info.lng) {
fillGPS()
}
if (!info.lng && info.bsLng) {
fillBase()
}
return this.createActiveInfoWindow(marker, map, infoDiv)
},
// 创建位置点弹框
createActiveInfoWindow(marker, map, div, isInfoOpen, isInfoAutoMove) {
var infoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(0, 0),
content: div,
autoMove: !!isInfoAutoMove,
})
console.log(infoWindow)
marker.infoWindow = infoWindow
if (isInfoOpen) {
infoWindow.open(map, marker.getPosition())
}
//给Marker绑定单击事件
marker.on('click', markerClick)
function markerClick(e) {
//infoWindow.setContent(e.target.content);
infoWindow.open(map, e.target.getPosition())
}
return infoWindow
},
// 打开激活位置点弹框
openActiveInfoWindow(map,makers, infoWindow) {
const MAP = map
const lnglat = makers.getPosition()
infoWindow.open(MAP, lnglat)
MAP.setZoomAndCenter(MAP.getZoom(), lnglat)
}
}
const mapService = new MapService();
export default mapService