historyLocation.js
5.37 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
import EventBus from '../event-bus'
import mapService from './mapService.js'
import MAPGEO from './mapgeo'
//历史停留
const historyLocation = {
data: [],
markers: {},
infowins: {},
bindEvent(){
EventBus.$on('uiMap.historyLocation.draw', (data)=>{
this.destroy();
const MAP = mapService.map;
this.data = data;
MAPGEO.transferAddressByGroup(this, ()=>{
EventBus.$emit('uiMap.historyLocation.transferSuccess', data);
this.data.forEach((item)=>{
let marker = mapService.createHistoryLocationMarker([item.elng, item.elat], MAP);
let infoWindow = mapService.bindLocationInfoWindow(marker, MAP, item);
this.markers[item.idx] = marker;
this.infowins[item.idx] = infoWindow;
});
console.log(this.markers)
mapService.setFitView(MAP);
this.createClusterer();
});
});
EventBus.$on('uiMap.historyLocation.destroy', ()=>{
this.destroy();
});
EventBus.$on('uiMap.historyLocation.openInfoWindow', (idx)=>{
if(this.markers[idx]){
const MAP = mapService.map;
const lnglat = this.markers[idx].getPosition();
this.infowins[idx].open(MAP, lnglat);
MAP.setZoomAndCenter(MAP.getZoom(), lnglat);
}
});
},
createClusterer(){
var cluster, markers = [];
for(let idx in this.markers){
markers.push(this.markers[idx]);
}
var self = this;
var map = mapService.map;
var count = markers.length;
var _renderClusterMarker = function (context) {
var factor = Math.pow(context.count / count, 1 / 18);
var div = document.createElement('div');
var Hue = 180 - factor * 180;
var bgColor = 'hsla(' + Hue + ',100%,50%,0.7)';
var fontColor = 'hsla(' + Hue + ',100%,20%,1)';
var borderColor = 'hsla(' + Hue + ',100%,40%,1)';
var shadowColor = 'hsla(' + Hue + ',100%,50%,1)';
div.style.backgroundColor = bgColor;
var size = Math.round(30 + Math.pow(context.count / count, 1 / 5) * 20);
div.style.width = div.style.height = size + 'px';
div.style.border = 'solid 1px ' + borderColor;
div.style.borderRadius = size / 2 + 'px';
div.style.boxShadow = '0 0 1px ' + shadowColor;
div.innerHTML = context.count;
div.style.lineHeight = size + 'px';
div.style.color = fontColor;
div.style.fontSize = '14px';
div.style.textAlign = 'center';
context.marker.setOffset(new AMap.Pixel(-size / 2, -size / 2));
context.marker.setContent(div)
};
addCluster(2);
function addCluster(tag) {
if (self.cluster) {
self.cluster.setMap(null);
}
if (tag == 2) {//完全自定义
self.cluster = new AMap.MarkerClusterer(map, markers, {
gridSize: 80,
maxZoom: 16,
renderClusterMarker: _renderClusterMarker
});
} else if (tag == 1) {//自定义图标
var sts = [{
url: "https://a.amap.com/jsapi_demos/static/images/blue.png",
size: new AMap.Size(32, 32),
offset: new AMap.Pixel(-16, -16)
}, {
url: "https://a.amap.com/jsapi_demos/static/images/green.png",
size: new AMap.Size(32, 32),
offset: new AMap.Pixel(-16, -16)
}, {
url: "https://a.amap.com/jsapi_demos/static/images/orange.png",
size: new AMap.Size(36, 36),
offset: new AMap.Pixel(-18, -18)
}, {
url: "https://a.amap.com/jsapi_demos/static/images/red.png",
size: new AMap.Size(48, 48),
offset: new AMap.Pixel(-24, -24)
}, {
url: "https://a.amap.com/jsapi_demos/static/images/darkRed.png",
size: new AMap.Size(48, 48),
offset: new AMap.Pixel(-24, -24)
}];
self.cluster = new AMap.MarkerClusterer(map, markers, {
styles: sts,
gridSize: 80
});
} else {//默认样式
self.cluster = new AMap.MarkerClusterer(map, markers, {gridSize: 80});
}
}
},
destroy: function(){
const MAP = mapService.map;
//MAP.clearMap();
this.data = [];
for(let idx in this.markers){
MAP.remove(this.markers[idx])
}
for(let idx in this.infowins){
MAP.remove(this.infowins[idx])
}
this.markers = {};
this.infowins = {};
if (this.cluster) {
this.cluster.setMap(null);
self.cluster = null;
}
this.clearTimeoutIds();
},
clearTimeoutIds(){
this.timeoutIds && this.timeoutIds.forEach((item)=>{
item && window.clearTimeout(item);
});
this.timeoutIds = []
}
}
export default historyLocation;