微信小程序登录+坐标定位显示km数

// 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
    })
  }

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值