mapgeo.js
2.98 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
const MAPGEO = {
geocoder: null,
getGeocoder: function () {
// let AMap = window.AMap;
return new Promise((resolve) => {
MAPGEO.geocoder = window.mapapi.invoke('Geocoder')
resolve(MAPGEO.geocoder)
// AMap.service("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++) {
let _lng = data[idx].lastlng || data[idx].blng;
let _lat = data[idx].lastlat || data[idx].blat;
// 防止出现gps点:.00000的错误
if (Number(_lng) > 0 && Number(_lat) > 0) {
lnglatArr.push([_lng, _lat]);
obj.realNum++;
}
// lnglatArr.push([
// data[idx].lastlng || data[idx].blng,
// data[idx].lastlat || data[idx].blat,
// ]);
}
(function (initIdx) {
geocoderGetAdr(lnglatArr, initIdx);
})(initIdx);
}
function geocoderGetAdr(arr, initIdx) {
const timeoutId = window.setTimeout(() => {
arr.forEach((item, idx) => {
window.mapapi.invoke('getLocation', window.mapapi.invoke('Point', item[0], item[1]), res => {
data[initIdx + idx].transferAddress = res.address;
obj.totalNum++;
if (obj.totalNum === obj.realNum) {
cb && cb(data);
//ifPlay && obj.init(data)
//EventBus.$emit('uiMap.replay.transferAdrDone');
}
})
});
// window.mapapi.invoke('getLocation', mapapi.invoke('Point', 116.404, 39.915))
// MAPGEO.geocoder.getAddress(arr, function (status, result) {
// if (status === "complete" && result.info === "OK") {
// //获取地址成功
// result.regeocodes.forEach((item, idx) => {
// data[initIdx + idx].transferAddress = item.formattedAddress;
// obj.totalNum++;
// });
// if (obj.totalNum === obj.realNum) {
// cb && cb(data);
// //ifPlay && obj.init(data)
// //EventBus.$emit('uiMap.replay.transferAdrDone');
// }
// } else {
// geocoderGetAdr(arr, initIdx);
// }
// });
});
obj.timeoutIds.push(timeoutId);
}
},
};
export default MAPGEO;