-
Notifications
You must be signed in to change notification settings - Fork 2
/
amapHot.js
237 lines (214 loc) · 7.1 KB
/
amapHot.js
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
;(function ($) {
var AMapHot = function ($this, options) {
var defaults = {
mapViewId: null,
zoom: 5,
center: [126.969383, 47.460428],
districtArea: '黑龙江',
mapInit: null,
mapZoomChange: null,
};
this.options = $.extend(defaults, options);
this.state = {
mapView: null,
adcodeProvinceObj: [], // 省级adcode
adcodeCityObj: [], // 地级市adcode
adcodeDistrictObj: [], // 区县adcode
adcodeAll: [], // 所有的adcode
district: null,
average: null,
defaultColor: 0xff6600,
};
this.$this = $this;
this._initMap();
this._initDistrict();
}
// 返回地级市adcode
AMapHot.prototype.returnCityAdcode = function () {
var state = this.state;
return state.adcodeCityObj.map((key) => key.adcode);
};
// 判断可视区域范围内的区县adcode
AMapHot.prototype.returnDistrictAdcode = function () {
var state = this.state;
var southwest = state.mapView.getBounds().getSouthWest(); // 获取西南角坐标
var northeast = state.mapView.getBounds().getNorthEast(); // 获取东北角坐标
var path = new AMap.Bounds(southwest, northeast);
var arr = state.adcodeDistrictObj.map(function(key) {
var lnglat = [key.lng, key.lat];
if (path.contains(lnglat)) {
return key.adcode;
}
return null;
});
return arr.filter(this._isAdcode);
};
// 行政区域查询
AMapHot.prototype.districtSearch = function (data, value) {
var state = this.state;
var $this = this;
// 清空地图覆盖物
state.mapView.clearMap();
this._average(data);
for (var i = 0; i < data.length; i++) {
var info = data[i];
state.district.setLevel(value); //行政区级别
state.district.setExtensions('all');
//按照adcode进行查询可以保证数据返回的唯一性
state.district.search((info.adcode).toString(), (function(info) {
return function(status, result) {
if (status === 'complete') {
$this._fileArea(result.districtList[0], info.num);
}
}
})(info));
}
};
// 根据地级市adcode查询出对应的区县的adcode
AMapHot.prototype.searchDistrictAdcode = function(adcode) {
var state = this.state;
var adcodeStr = adcode.toString();
var arr;
for (var i = 0; i < state.adcodeAll.length; i++) {
var obj = state.adcodeAll[i];
if (obj.adcode === adcodeStr) {
arr = obj.arr;
break;
}
}
return arr;
};
// 设置地图层级
AMapHot.prototype.setMapZoom = function (zoomIndex) {
var options = this.options;
options.mapView.setZoom(zoomIndex);
};
// 初始化地图
AMapHot.prototype._initMap = function () {
var state = this.state;
var options = this.options;
var $this = this.$this;
var mapViewId = $this[0].id;
if (mapViewId !== undefined) {
state.mapView = new AMap.Map(mapViewId, {
resizeEnable: true,
center: options.center,
zoom: options.zoom,
});
// 地图层级改变后触发事件
state.mapView.on('zoomchange', function () {
if (options.mapZoomChange) {
options.mapZoomChange(state.mapView.getZoom());
}
})
}
};
// 获取指定区域一系列的adcode
AMapHot.prototype._initDistrict = function () {
var options = this.options;
var state = this.state;
//行政区划查询
var opts = {
subdistrict: 2, // 返回下一级行政区
showbiz:false // 最后一级返回街道信息
};
state.district = new AMap.DistrictSearch(opts);//注意:需要使用插件同步下发功能才能这样直接使用
state.district.search(options.districtArea, function(status, result) {
if(status=='complete'){
var optionsFirst = result.districtList;
if (optionsFirst !== undefined) {
for (var i = 0; i < optionsFirst.length; i++) {
var firstInfo = optionsFirst[i];
state.adcodeProvinceObj.push({
adcode: firstInfo.adcode,
level: firstInfo.level,
lng: firstInfo.center.lng,
lat: firstInfo.center.lat,
name: firstInfo.name,
});
var optionsSecond = firstInfo.districtList;
if (optionsSecond !== undefined) {
for (var j = 0; j < optionsSecond.length; j++) {
var secondInfo = optionsSecond[j];
state.adcodeCityObj.push({
adcode: secondInfo.adcode,
level: secondInfo.level,
lng: secondInfo.center.lng,
lat: secondInfo.center.lat,
name: secondInfo.name,
});
var optionsThird = secondInfo.districtList;
if (optionsSecond !== undefined) {
var adcodeArr = [];
for (var s = 0; s < optionsThird.length; s++) {
var thirdInfo = optionsThird[s];
state.adcodeDistrictObj.push({
adcode: thirdInfo.adcode,
level: thirdInfo.level,
lng: thirdInfo.center.lng,
lat: thirdInfo.center.lat,
name: thirdInfo.name,
});
adcodeArr.push(thirdInfo.adcode);
}
state.adcodeAll.push(
{
adcode: secondInfo.adcode,
arr: adcodeArr,
}
);
}
}
}
}
}
if (options.mapInit) options.mapInit();
// console.log('state.adcodeProvinceObj', state.adcodeProvinceObj);
// console.log('state.adcodeCityObj', state.adcodeCityObj);
// console.log('state.adcodeDistrictObj', state.adcodeDistrictObj);
}
});
};
AMapHot.prototype._isAdcode = function (value) {
return value !== null;
};
// 计算传入数量的平均值
AMapHot.prototype._average = function (data) {
var state = this.state;
var arr = 0;
for (var i = 0; i < data.length; i++) {
var info = data[i];
arr += info.num;
}
state.average = arr / data.length;
}
// 计算颜色值
AMapHot.prototype._colorValue = function (num) {
var state = this.state;
var times = num / state.average;
var rand = Math.floor(times * 0x0000ff + state.defaultColor).toString(16);
return '#' + rand;
}
// 区域画图和填充颜色
AMapHot.prototype._fileArea = function (data, num) {
var state = this.state;
var colorValue = this._colorValue(num);
var bounds = data.boundaries;
if (bounds) {
for (var i = 0, l = bounds.length; i < l; i++) {
new AMap.Polygon({
map: state.mapView,
strokeWeight: 1,
strokeColor: colorValue,
fillColor: colorValue,
fillOpacity: 0.6,
path: bounds[i],
});
}
// state.mapView.setFitView();//地图自适应
}
};
$.fn.amapHot = function (options) {
return new AMapHot(this, options);
};
})(jQuery)