mapBgeo.js
5.61 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
/*
* @Author: 'wangzhihui' '1733904062@qq.com'
* @Date: 2024-11-28 16:37:15
* @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @LastEditTime: 2025-02-14 15:00:02
* @FilePath: \oEshield\src\utils\mapgeo.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
const MAPGEO = {
geocoder: null,
getGeocoder: function () {
return new Promise((resolve) => {
var geocoder= AMap.invoke('Geocoder');
// AMap.plugin('AMap.Geocoder', function () {//回调函数
// var geocoder = new AMap.Geocoder({
// batch: true
// });
MAPGEO.geocoder = geocoder;
resolve(geocoder);
});
// })
},
transferAddressByGroup(obj, cb) {
console.log(obj,'obj')
if (!this.geocoder) {
this.getGeocoder().then(() => {
this.transfer(obj, cb)
})
} else {
this.transfer(obj, cb)
}
},
transfer(obj, cb) {
const data = obj.data;
obj.totalNum = 0;
obj.realNum = 0; //为了剔除脏数据,脏数据导致解析不回调
data.forEach(item => {
if (Number(item.blng) > 0 && Number(item.blat) > 0) {
obj.realNum++;
}
})
// console.log(data,'data')
// Promise.race([geocoderGetAdr(data),limitload()])
geocoderGetAdr(data)
function fixed6(num) {
return Number(num).toFixed(6);
}
// 循环设置地址,修复数组中相同经纬度只设置第一个的bug jack 20230720
function setAddress(row, item) {
const {content: {poi_desc, address_detail: {province, city, district, town, street}}} = item
const _list = data.filter(
(k) =>
fixed6(k.blng) == fixed6(row.lng) && fixed6(k.blat) == fixed6(row.lat)
)
_list.map((k) => {
k.transferAddress = province + city + district + town + street + poi_desc
})
}
function geocoderGetAdr(arr, initIdx) {
return new Promise(async (resolve, reject) => {
if (!arr || arr.length <= 0) {
return false;
}
for(const item of arr){
try {
const o = await AMap.invoke('Point', item.blng, item.blat);
await new Promise(resolve => setTimeout(resolve, 10))
MAPGEO.geocoder.getLocation(o,(res)=>{
setAddress(o, res);
obj.totalNum++;
// console.log(obj.totalNum , obj.realNum,obj.totalNum === obj.realNum)
if (obj.totalNum === obj.realNum && typeof cb === 'function') {
cb(data);
resolve()
}
});
// const res=await new Promise((resolve)=>{
// MAPGEO.geocoder.getLocation(o,(res)=>{
// resolve(res)
// });
// })
// console.log(res,obj.totalNum , obj.realNum,obj.totalNum === obj.realNum)
// setAddress(o, res);
// obj.totalNum++;
// // console.log(obj.totalNum , obj.realNum,obj.totalNum === obj.realNum)
// if (obj.totalNum === obj.realNum && typeof cb === 'function') {
// cb(data);
// resolve()
// }
} catch (error) {
console.error(`Error processing item ${item}:`, error);
}
}
})
}
function limitload(){
// let time=data.length*1000;
// new Promise(resolve => setTimeout(()=>{
// if ( typeof cb === 'function') {
// cb(data);
// resolve()
// }
// }, time))
}
}
// transfer(obj, cb) {
// const data = obj.data;
// obj.count=0;
// obj.len = data.length;
// obj.times = obj.len > 20 ? Math.ceil(obj.len / 20) : 1;
// obj.totalNum = 0;
// obj.realNum = 0; //为了剔除脏数据,脏数据导致解析不回调
// for (let i = 0; i < obj.times; i++) {
// let lnglatArr = [];
// let initIdx = i * 20;
// let maxIdx = i === obj.times - 1 ? obj.len : (i + 1) * 20;
// for (let idx = 0; idx < obj.len; idx++) {
// // 防止出现gps点:.00000的错误
// if (Number(data[idx].blng) > 0 && Number(data[idx].blat) > 0) {
// lnglatArr.push([data[idx].blng, data[idx].blat])
// obj.realNum++;
// }
// }
// (function (initIdx) {
// geocoderGetAdr(lnglatArr, initIdx)
// // })(initIdx)
// }
// function fixed6(num) {
// return Number(num).toFixed(6);
// }
// // 循环设置地址,修复数组中相同经纬度只设置第一个的bug jack 20230720
// function setAddress(row, item) {
// const {content: {poi_desc, address_detail: {province, city, district, town, street}}} = item
// const _list = data.filter(
// (k) =>
// fixed6(k.blng) == fixed6(row.lng) && fixed6(k.blat) == fixed6(row.lat)
// )
// _list.map((k) => {
// k.transferAddress = province + city + district + town + street + poi_desc
// })
// }
// async function geocoderGetAdr(arr, initIdx) {
// obj.count+=arr.length
// if (!arr || arr.length <= 0) {
// return false;
// }
// // for(const item of arr){
// let i=0;
// for(;i < arr.length; i++){
// let item=arr[i]
// try {
// const o = await AMap.invoke('Point', ...item);
// const res = await new Promise((resolve)=>{
// MAPGEO.geocoder.getLocation(o,(res)=>{
// console.log('getLocation',res,i)
// if (!res) {
// throw new Error('Invalid result from getLocation');
// }
// resolve(res)
// });
// })
// // console.log(o, res,i)
// // setAddress(o, res);
// obj.totalNum++;
// console.log(obj.totalNum , obj.realNum,obj.totalNum === obj.realNum,obj.count)
// if (obj.totalNum === obj.realNum && typeof cb === 'function') {
// cb(data);
// }
// } catch (error) {
// console.error(`Error processing item ${item}:`, error);
// }
// }
// }
// }
}
export default MAPGEO