map.js
2.18 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
// 引入SDK核心类
// import QQMapWX from '../../../utils/qqmap-wx-jssdk.min.js'
const map_key = 'ZEJBZ-LVNRG-EXMQH-I74N6-EHNIS-WHF6E'
// 实例化API核心类
// const myMap = new QQMapWX({
// key: map_key // 必填
// })
/**逆地址解析 */
const detailedAdrress = (tlng, tlat, da, fa, type) => {
if (tlng && tlat) {
let coord_type = 5;
if (null != type) {
coord_type = type
}
wx.showToast({
title: tlng,
})
// myMap.reverseGeocoder({
// location: {
// latitude: tlat,
// longitude: tlng
// },
// coord_type: coord_type,
// success(res) {
// if (res.status == "0") {
// if (da && typeof da === 'function') {
// da(res.result)
// }
// }
// },
// fail(res) {
// if (fa && typeof fa === 'function') {
// fa(res)
// }
// }
// })
}
}
/**获取搜索周边信息 */
const searchAddress = (address,lat,lng,callBack,error) =>{
if(!address) {
myMap.reverseGeocoder({
location: {
latitude: lat,
longitude: lng
},
success(res){
search(res.result.address,lat,lng,callBack,error)
}
})
}else{
search(address,lat,lng,callBack,error)
}
}
const search = (address,lat,lng,callBack,error) => {
myMap.search({
keyword: address,
location: `${lat},${lng}`,
success(res) {
callBack(res)
},
fail(err) {
error(err)
}
})
}
/**两点之间的距离 */
const calculateDistance = (flng, flat, tlng, tlat, cb) => {
// console.log(flng + ","+ flat+","+ tlng+","+ tlat);
//默认计算步行距离
myMap.calculateDistance({
from: {
latitude: flat,
longitude: flng
},
to: [{
latitude: tlat,
longitude: tlng
}],
success: function (res) {
if (res.status == "0") {
if (cb && typeof cb === 'function') {
cb(res.result.elements[0].distance)
}
}
},
fail: function (res) {
console.log(res)
}
})
}
module.exports = {
detailedAdrress,
calculateDistance,
searchAddress
}