mapBgeo.js
3.26 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
/*
* @Author: 'wangzhihui' '1733904062@qq.com'
* @Date: 2024-11-28 16:37:15
* @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @LastEditTime: 2025-01-14 17:05:20
* @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) => {
console.log(window)
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) {
if (!this.geocoder) {
this.getGeocoder().then(() => {
this.transfer(obj, cb)
})
} else {
this.transfer(obj, cb)
}
},
transfer(obj, cb) {
const data = obj.data;
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 = initIdx; idx < maxIdx; 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
})
}
function geocoderGetAdr(arr, initIdx) {
if (!arr || arr.length <= 0) {
return false;
}
const timeoutId = window.setTimeout(() => {
arr.forEach(item=>{
let o=AMap.invoke('Point',...item)
MAPGEO.geocoder.getLocation( o,function(res){
console.log(res,'res')
setAddress(o, res);
obj.totalNum++;
if (obj.totalNum === obj.realNum) {
console.log('geocoderGetAdr', data)
cb && cb(data)
}
})
})
// MAPGEO.geocoder.getAddress(arr, function (status, result) {
// if (status === 'complete' && result.info === 'OK') {//获取地址成功
// result.regeocodes.forEach((item, idx) => {
// let o = arr[idx]
// setAddress(o, item);
// obj.totalNum++;
// });
// if (obj.totalNum === obj.realNum) {
// cb && cb(data)
// }
// } else {
// // geocoderGetAdr(arr, initIdx)
// }
// });
});
if (!obj.timeoutIds) {
obj.timeoutIds = []
}
obj.timeoutIds.push(timeoutId)
}
}
}
export default MAPGEO