js部分:
data: {
cityname: '未选择',
theatreList: [] // 保存影院列表
},
/** 点击实现搜索当前城市下的影院列表 */
tapSearch(){
let qqmapsdk = getApp().globalData.qqmapsdk;
qqmapsdk.search({
keyword: '影院',
region: this.data.cityname,
page_size: 20,
success: (res)=>{
console.log(res)
res.data.forEach(item=>{
item._distance_ = (item._distance/1000).toFixed(2)
})
this.setData({
theatreList: res.data
})
}
})
},
tapItem(event){
let index = event.currentTarget.dataset.index;
let t = this.data.theatreList[index];
wx.openLocation({
latitude: t.location.lat,
longitude: t.location.lng
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// 加载当前位置的周边影院
let qqmapsdk = getApp().globalData.qqmapsdk;
qqmapsdk.search({
keyword: '影院',
page_size: 20,
success: (res)=>{
console.log(res)
res.data.forEach(item=>{
// (item._distance/1000).toFixed(2) : 因为模板中不能写方法
item._distance_ = (item._distance/1000).toFixed(2)
})
this.setData({
theatreList: res.data
})
}
})
},
补充( 因为可能会更换新城市, 所以每次onShow时要执行以下代码 ):
onShow: function () {
// 更新左上角的位置字符串
let name = getApp().globalData.CITYNAME;
this.setData({
cityname: name
})
},