search.js
6.2 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// pages/reserve/search.js
// 引入SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
key: 'SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ' // 必填
});
import util from '../../utils/util';
const {regeneratorRuntime} = global
const search_history_cache_key='SEARCH_HISTORY_CACHE_KEY';
Page({
/**
* 页面的初始数据
*/
data: {
history:[],
suggestion:[],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getHistory();
this.getPos()
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
getHistory(){
let ca=util.cache(search_history_cache_key);
if(ca){
this.setData({
history:ca
})
}
},
setHistory(item){
let newca=[item];
let ca=this.data.history;
let maxL=20;
for(let i=0;i<ca.length;i++){
if(ca[i].id!=item.id){
newca.push(ca[i]);
}
if(newca.length>maxL) break;
}
util.cache(search_history_cache_key,newca);
this.setData({
history:newca,
})
},
//在Page({})中使用下列代码
//数据回填方法
backfill: function (e) {
var id = e.currentTarget.id;
for (var i = 0; i < this.data.suggestion.length; i++) {
if (i == id) {
console.log(this.data.suggestion[i])
this.setHistory(this.data.suggestion[i])
let pages = getCurrentPages();
let p = pages[pages.length - 2];
p.setSuggestion(this.data.suggestion[i])
//util.cache('suggestion',this.data.suggestion[i]);
this.setData({
backfill: this.data.suggestion[i].title
});
wx.navigateBack();
}
}
},
backHistoryFill: function (e) {
var i = util.attr(e,'index');
let item=this.data.history[i];
console.log(item)
this.setHistory(item)
this.setData({
backfill: item.title
});
let pages = getCurrentPages();
let p = pages[pages.length - 2];
p.setSuggestion(item)
wx.navigateBack();
},
inputVal:'',
//触发关键词输入提示事件
getsuggest: function (e) {
var _this = this;
//调用关键词提示接口
this.inputVal=e.detail.value;
if(e.detail.value.length<1){
return this.setData({
suggestion: []
})
}
qqmapsdk.getSuggestion({
//获取输入框值并设置keyword参数
keyword: e.detail.value, //用户输入的关键词,可设置固定值,如keyword:'KFC'
region:this.location.city||'', //设置城市名,限制关键词所示的地域范围,非必填参数
//region_fix:1,//锁定在本市
//filter:'category<>美食',
success: function (res) {//搜索成功后的回调
console.log(res);
var sug = [];
for (var i = 0; i < res.data.length; i++) {
sug.push({ // 获取返回结果,放到sug数组中
title: res.data[i].title,
id: res.data[i].id,
addr: res.data[i].address,
city: res.data[i].city,
district: res.data[i].district,
latitude: res.data[i].location.lat,
longitude: res.data[i].location.lng
});
}
_this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
suggestion: sug
});
},
fail: function (error) {
_this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
suggestion: []
});
console.error(error);
},
complete: function (res) {
console.log('getsuggest',res);
}
});
},
nearby_search: function () {
var _this = this;
// 调用接口
let location='';
if(this.data.suggestion.length>0){
location=this.data.suggestion[0]
}
qqmapsdk.search({
keyword: this.inputVal, //搜索关键词
region:this.location.city||'', //设置城市名,限制关键词所示的地域范围,非必填参数
page_size:20,
location:location,
//filter:'category<>美食',
success: function (res) { //搜索成功后的回调
var mks = []
for (var i = 0; i < res.data.length; i++) {
mks.push({ // 获取返回结果,放到mks数组中
title: res.data[i].title,
id: res.data[i].id,
addr: res.data[i].address,
city: res.data[i].city,
district: res.data[i].district,
latitude: res.data[i].location.lat,
longitude: res.data[i].location.lng,
})
}
_this.setData({ //设置suggestion属性,将关键词搜索结果以列表形式展示
suggestion:mks,
});
},
fail: function (res) {
console.log(res);
},
complete: function (res) {
console.log('nearby_search',res);
}
});
},
location:'',
getPos(){
this._getPos().then(res => {
console.log('wx.getLocation.then', res);
this.location=res
this.getCity()
}).catch((e) => {
console.log('wx.getLocation.catch', e);
if ('getLocation:fail auth deny' == e.errMsg || 'getLocation:fail:auth denied' == e.errMsg) {
this.selectComponent('#showModal').options({
header: '定位失败',
body: '需要地理位置授权'
}).show();
} else {
setTimeout(() => {
this.getPos()
}, 500)
}
})
},
_getPos(){
return new Promise((success, fail) => {
wx.getLocation({
type: 'gcj02',
success: (res) => {
success(res)
},
fail: (res) => {
fail(res)
},
complete: (res) => {
console.log('wx.getLocation', res)
}
});
})
},
getCity(){
qqmapsdk.reverseGeocoder({
location:this.location,
success:(res)=>{
this.location['city']='';
if(res.status==0){
this.location['city']=res.result.ad_info.city
}
},
})
}
})