location.js
3.14 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
// components/2.0/filter/location.js
import DA from '../../../pages/reserve/data'
import util from '../../../utils/util'
import QQMapWX from '../../../utils/qqmap-wx-jssdk.js'
const qqmapsdk = new QQMapWX({
key: 'SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ'
});
const CACHE_LOCALTION_KEY='CACHE_LOCALTION_KEY'
Component({
/**
* 组件的属性列表
*/
properties: {
code:String,
},
/**
* 组件的初始数据
*/
data: {
address:'',
activeCity:{},
hasShow:false,
},
attached(){
this.init()
},
/**
* 组件的方法列表
*/
methods: {
async init(){
this.position={}
this.isAddr=false
this.useDefTS=false
this.citys=await DA.getCitys()
this.cacheCity=null
this.setData({
citys: this.citys
})
let cache=util.cache(CACHE_LOCALTION_KEY)
if(cache && cache.city){
this.cacheCity=cache.city
}
this.selectComponent('#getpos').getPos();
},
rePos(e){
this.cacheCity=null
this.isAddr=util.attr(e,'isAddr') && !this.useDefTS
this.selectComponent('#getpos').getPos();
},
hide(){
this.setData({
hasShow:false,
})
},
show(){
this.setData({
hasShow:true,
})
},
event(data){
data.position=this.position
this.triggerEvent('event',data)
util.cache('CACHE_LOCALTION_KEY',data)
},
checkedCity(e){
let activeCity=this.citys[util.attr(e,'index')]
this.setData({
activeCity:activeCity,
})
this.event({city:activeCity,address:this.position.address,isAddr:false})
this.hide()
},
onpos(e) {
let res = e.detail;
console.log('onpos.res', res);
let useDef=false
if(res.latitude==''){//默认使用唐山
this.useDefTS=true
res.latitude=39.684671
res.longitude=118.176300
}
this.position.latitude = res.latitude;
this.position.longitude = res.longitude;
qqmapsdk.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success:(res)=>{
if(res.status!=0) return;
console.log('qqmapsdk.reverseGeocoder',res.result)
this.position.address_component= res.result.address_component
this.position.address=this.useDefTS?'':res.result.address
let activeCity={}
if(this.cacheCity){
activeCity=this.cacheCity
}else if(!this.data.code){
this.citys.some(ci=>{
if(ci.name==this.position.address_component.city){
activeCity=ci
return true
}
})
}else{
this.citys.some(ci=>{
if(ci.code==this.data.code){
activeCity=ci
return true
}
})
}
this.setData({
activeCity:activeCity,
address:this.position.address
})
this.event({city:activeCity,address:this.position.address,isAddr:this.isAddr})
if(this.isAddr){//点击地址
this.hide()
}
},
})
},
}
})