// pages/list/mine.js
Page({
/**
* 页面的初始数据
*/
data: {
userInfo: {
nickName: '',
avatarUrl: ''
},
message: '',
address: '',
shops: [
]
},
//选择地址
chooseLocation() {
wx.chooseLocation({
success: ({ address }) => {
this.setData({
address
})
}
})
},
//登录方式
login() {
//获取微信用户信息
wx.getUserProfile({
desc: '实现登录功能',
success: ({ userInfo: { nickName, avatarUrl } }) => {
//获取用户信息
this.data.userInfo = {
nickName, avatarUrl
}
//更新 页面
this.setData({
userInfo: this.data.userInfo
})
//要将对象转为字符串保存
wx.setStorageSync('userInfo', JSON.stringify(this.data.userInfo))
}
})
},
//退出方法
async exit() {
await wx.$confirm('确定退出吗?')
this.data.userInfo = {
nickName: '',
avatarUrl: ''
}
this.setData({
userInfo: this.data.userInfo
})
wx.removeStorageSync('userInfo')
},
//将用户信息缓存到本地
onLoad() {
if (wx.setStorageSync('userInfo')) {
this.data.userInfo = JSON.parse(wx.setStorageSync('userInfo'))
this.setData({
userInfo: this.data.userInfo
})
}
},
//扫码
scan() {
wx.scanCode({
success: ({ result }) => {
this.setData({
message: result
})
}
})
},
//电话
phone() {
wx.makePhoneCall({
phoneNumber: '10000',
success: (result) => {
// console.log(result);
}
})
},
async search() {
//后台返回一份数据
let arr = [
{
id: 1,
name: "新街口店",
address: "江苏省南京市玄武区中山路18号"
},
{
id: 2,
name: "江宁店",
address: "江苏省南京市江宁区齐武路"
},
{
id: 3,
name: "青岛店",
address: "山东省青岛市市北区杭州路3号戊"
},
{
id: 4,
name: "北京店",
address: "北京市西城区南横东街1号"
},
]
//定义一个存放promise对象的数组
let ps = []
//获取所有地址的坐标
arr.forEach(r => {
let p = wx.$request('https://apis.map.qq.com/ws/geocoder/v1/', {
address: r.address,
key: '7W4BZ-D35WU-TFUV3-BUP7Z-NWSD2-GJF6Q'
})
ps.push(p)
})
let res = await Promise.all(ps)
let to = res.map(r => {
let obj = r.result.location
return obj.lat + ',' + obj.lng
}).join(';')
//console.log(to);
//获取当前位置坐标
let from = await wx.$getLocation()
let { result:{rows} } = await wx.$request('https://apis.map.qq.com/ws/distance/v1/matrix', {
from,
to,
key: '7W4BZ-D35WU-TFUV3-BUP7Z-NWSD2-GJF6Q'
})
//console.log(rows);
let { elements } = rows[0]
elements.forEach((r, i) => {
arr[i].distance = (r.distance / 1000).toFixed(2) + "km"
arr[i].duration = (r.duration / 60 / 60).toFixed(2) + "h"
})
this.data.shops = arr
this.setData({
shops: this.data.shops
})
}
})
微信小程序登录+坐标定位显示km数
最新推荐文章于 2024-06-12 09:57:47 发布