微信小程序也可以实现定位打卡/签到打卡了(附源码)

简介: 本篇文章带你实现实时定位功能:包括实时定位监听、定位权限判断、经纬度间距计算、判断当前位置是否在目的地的范围区间。

主要应用场景包括但不限于:定位打卡。


1、在 package.json 文件中添加导入文件


"dependencies": {
  "@rattan/location":"1.0.2",
},

2、代码示例


<template>
  <view class="page-content">
  <view style="padding-top: 200rpx;">
  </view>
  <view style="margin: 20rpx;padding: 10rpx;background: #FFFFFF;">
    <view style="padding: 20rpx;color: 666666;">
    <view style="color: #000000;">
      1、当前经纬度:
    </view>
    <view style="margin-left: 20rpx;">
      经度:{{cData.curPos.lon}}
    </view>
    <view style="margin-left: 20rpx;">
      纬度:{{cData.curPos.lat}}
    </view>
    <view style="padding: 20rpx;color: #000000;">
      2、目的地经纬度:
    </view>
    <view style="margin-left: 20rpx;">
      经度:{{cData.desPos.lon}}
    </view>
    <view style="margin-left: 20rpx;">
      纬度:{{cData.desPos.lat}}
    </view>
    </view>
    <view style="padding: 20rpx;color: 666666;">
    <view style="color: #000000;">
      3、设定{{cData.rangeMeter}}米属于范围区间内:
    </view>
    <view style="margin-left: 20rpx;">
      距离目的地:{{cData.curDis}}米
    </view>
    <view style="margin-left: 20rpx;">
      {{cInRangeStr}}
    </view>
    </view>
    <view style="padding: 20rpx;">
    <view v-if="cNOLocationTips.length > 0" style="color: #000000;">
      4、未权限提示:
    </view>
    <view v-for="(item,index) in cNOLocationTips" :key="index">
      <view class="auth-tips" style="margin-left: 20rpx;">
      {{item}}
      </view>
    </view>
    </view>
  </view>
  </view>
</template>
<script>
  import func from "@rattan/location/func.js"
  import zmLoc from "@rattan/location/index.js"
  export default {
  components: {
  },
  data() {
    return {
    cData: { // 自定义判断字段
      curPos: { // 当前经纬度定位
      lat: 24.46667,
      lon: 118.184761,
      },
      desPos: { // 目的地经纬度
      lat: 24.490107,
      lon: 118.184861,
      },
      curDis: 10000, //与目标距离  单位:米
      rangeMeter: 1000, //范围区间,单位:米
      isInRange: false, //是否在打卡范围内
      // ------ 定位启闭状态 ------
      isLocationMobileSystem: true, // 手机系统
      isLocationWechatApp: true, // 微信app,如果手机系统定位关闭,那么微信app定位也是关闭状态
      isLocationByMiniProgram: true, // 微信小程序
    },
    }
  },
  computed: {
    // 定位没有打开提示文案
    cNOLocationTips() {
    let _arr = []
    if (this.cData.isLocationMobileSystem == false) {
      _arr.push("* 请打开手机和微信应用定位服务")
    } else if (this.cData.isLocationMobileSystem == true &&
      this.cData.isLocationWechatApp == false) {
      _arr.push("* 请打开微信应用定位服务")
    }
    if (this.cData.isLocationByMiniProgram == false) {
      _arr.push(
      "* 请打开小程序定位服务,点击右上角···,点击设置"
      )
    }
    return _arr
    },
    // 是否进入区间范围提示
    cInRangeStr() {
    return this.cData.isInRange ? "已进入范围区间" : "未进入范围区间";
    },
  },
  onLoad(options) {
    this.addMonitor()
  },
  onUnload(options) {
    this.removeMonitor()
  },
  onShow(options) {
    this.openPosition();
  },
  onHide(options) {
    this.closePosition();
  },
  methods: {
    // 开启定位
    openPosition() {
    const systemInfo = uni.getSystemInfoSync()
    // 模拟器没有这两个字段,设置默认打开
    if (systemInfo.locationEnabled != undefined &&
      systemInfo.locationAuthorized != undefined) {
      // 手机系统定位开关
      this.cData.isLocationMobileSystem = systemInfo.locationEnabled;
      // 微信app定位开关,如果手机系统定位关闭,那么微信app定位也会一起关闭
      this.cData.isLocationWechatApp = systemInfo.locationAuthorized
      console.log("手机系统定位开关:", this.cData.isLocationMobileSystem)
      console.log("微信应用定位开关:", this.cData.isLocationWechatApp)
    }
    zmLoc.zmStartMonitor()
    },
    // 关闭定位
    closePosition() {
    zmLoc.zmEndMonitor();
    },
    // 添加监听
    addMonitor() {
    let that = this;
    // 定位监听 - 成功回调
    uni.$on('iAuthorityOpen', (res) => {
      console.log("小程序定位开关:", res)
      if (that.cData.isLocationByMiniProgram != res) {
      that.cData.isLocationByMiniProgram = res;
      }
    })
    // 定位监听 - 成功回调
    uni.$on('iLocationSuc', (res) => {
      console.log('iLocationSuc:', JSON.stringify(res))
      if (!that.$u.test.isEmpty(res)) {
      that.cData.curPos = {
        lat: res?.latitude,
        lon: res?.longitude,
      }
      }
      if (!that.$u.test.isEmpty(that.cData.desPos) &&
      !that.$u.test.isEmpty(that.cData.curPos)) {
      that.checkDistanceAndRange()
      } else {
      console.log("======没有返回经纬度对象")
      that.cData.isInRange = false;
      }
    })
    // 定位监听 - 失败回调
    uni.$on('iLocationErr', (e) => {
      console.log("iLocationErr_e:", JSON.stringify(e));
    })
    },
    // 移除监听
    removeMonitor() {
    zmLoc.zmEndMonitor();
    // 结束定位监听
    uni.$off('iAuthorityOpen');
    uni.$off('iLocationSuc');
    uni.$off('iLocationErr');
    },
    // 计算距离和是否在打卡区间
    checkDistanceAndRange() {
    // 单位:米
    this.cData.curDis = (func.zmCalcuDistance(
      this.cData.curPos.lat,
      this.cData.curPos.lon,
      this.cData.desPos.lat,
      this.cData.desPos.lon) * 1000).toFixed(1)
    console.log("curDis:", this.cData.curPos.lat, ",", this.cData.curPos.lon)
    console.log("desDis:", this.cData.desPos.lat, ",", this.cData.desPos.lon)
    console.log("距离:", JSON.stringify(this.cData.curDis), "米")
    this.cData.isInRange = this.cData.curDis <= this.cData.rangeMeter ? true : false;
    },
  },
  }
</script>
<style lang="scss" scoped>
  .page-content {
  width: 100%;
  height: 100%;
  background: #F5F9FC;
  .auth-tips {
    padding-bottom: 12rpx;
    width: 100%;
    text-align: left;
    font-size: 24rpx;
    color: #FF0000;
  }
  }
</style>


源码/demo展示:


1、gitee:https://gitee.com/chenzm_186/demo-real-time-location-mini

2、csdn:https://download.csdn.net/download/weixin_38633659/85436545


相关文章
|
1月前
|
存储 小程序 算法
东郊到家预约系统开发|源码案例|小程序
区块链的最重要特性是去中心化,它不依赖于任何中心机构或第三方信任
|
1月前
|
小程序 JavaScript 前端开发
基于微信小程序的商城购物系统的设计与实现(论文+源码)_kaic
基于微信小程序的商城购物系统的设计与实现(论文+源码)_kaic
|
19天前
|
人工智能 小程序 Java
Java智慧校园系统源码 微信小程序+电子班牌
通过设备管理对百纳智慧校园的智慧班牌以及百纳智慧屏(校牌)进行统一集中式管理,支持浏览所有设备的基本信息以及在离线状态,支持添加设备、设备一键开关机、一键重启、设置节假日开关机时间、设置日常开关机时间、远程班牌截屏、远程班牌升级等操作。
|
1月前
|
小程序 JavaScript 数据安全/隐私保护
分享全栈开发医疗小程序 -带源码课件(课件无解压密码),自行速度保存
看到好多坛友都在求SpringBoot2.X + Vue + UniAPP,全栈开发医疗小程序 - 带源码课件,我看了一下,要么链接过期,要么课件有压缩密码。特意整理了一份分享给大家,个人认为还是比较全面的。希望对大家有所帮助!课程仅供大家学习交流使用!
37 1
分享全栈开发医疗小程序 -带源码课件(课件无解压密码),自行速度保存
|
1月前
|
编解码 小程序 算法
短剧系统开发(网页版/APP/小程序)丨短剧系统开发运营版及源码出售
短剧系统开发功能旨在为用户提供观看、分享和交流短剧作品的平台,涉及多种功能和特性,
|
1月前
|
移动开发 负载均衡 小程序
代驾app开发丨代驾系统开发玩法详情丨代驾系统开发网页版/H5/小程序及源码部署
**司机/代驾员端**:司机可以通过APP接收订单,查看订单详情、路线和导航,提供现场服务并进行确认。
|
1月前
|
小程序 搜索推荐 算法
微信小程序外卖管理的设计与实现(论文+源码)_kaic
微信小程序外卖管理的设计与实现(论文+源码)_kaic
|
1月前
|
监控 小程序 前端开发
基于微信小程序充电桩预约管理系统的设计与实现(论文+源码)_kaic
基于微信小程序充电桩预约管理系统的设计与实现(论文+源码)_kaic
|
1月前
|
小程序 安全 前端开发
基于微信小程序的英语单词记忆系统的设计与实现(论文+源码)_kaic
基于微信小程序的英语单词记忆系统的设计与实现(论文+源码)_kaic
|
1月前
|
关系型数据库 MySQL Java
微信仿真平台的设计和实现(设计+源码)_kaic
微信仿真平台的设计和实现(设计+源码)_kaic

热门文章

最新文章