小程序切换前后台获取实时地理信息进行定位上报

在做小程序有个需求要采集司机定位并进行上报给后台,之前用了wx.getLocation() 和定时调用setinterval在一定的间隔内进行采集和上报司机位置,但是频繁调用时它给我返回报错信息 官网解释 高频率调用会导致耗电 ,而且2.17.0 增加调用频率限制,相关公告
后面使用了wx.startLocationUpdateBackground 和 wx.onLocationChange 不管是切换前后后都能获取地理位置信息。

1.getLocation 的使用

首先在微信公众平台 开发管理 -> 接口设置里面去申请开通该接口
在这里插入图片描述
其次在app.json里面配置permission,意为申请权限

"permission": {
    
    
   "scope.userLocation": {
    
    
     "desc": "你的位置信息将用于定位"
   }
 },
 "requiredPrivateInfos": [
   "getLocation",
 ]

这样就可以使用wx.getLocation 获取地理位置信息
代码如下:

// 封装 getLocation 方法  在需要获取地理位置信息 调用该方法 就可以获取当前地理位置信息
 getLocation() {
    
    
    return new Promise((resolve, reject) => {
    
    
      wx.getLocation({
    
    
        type:'gcj02',
        isHighAccuracy: true,
        success: res => {
    
    
          resolve(res)
        },
        fail: e => {
    
    
           wx.getSetting({
    
    
            success: res => {
    
    
              if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
    
    
                wx.showModal({
    
    
                  title: '提示',
                  content: '您拒绝了定位权限,将无法使用该功能',
                  success: res => {
    
    
                    if (res.confirm) {
    
    
                      wx.openSetting({
    
    
                        success: res => {
    
    
                          if (res.authSetting['scope.userLocation']) {
    
    
                            wx.getLocation({
    
    
                              type:'gcj02',
                              isHighAccuracy: true,
                              success: res => {
    
    
                                resolve(res)
                              }
                            });
                          } else {
    
    
                            wx.showToast({
    
    
                              title: '您拒绝了定位权限,将无法使用该功能',
                              icon: 'none'
                            });
                          }
                        }
                      });
                    }
                  }
                });
              } else {
    
    
                resolve(res)
              }
            }
          });
        }
      })
    })
  },

// 需要获取地理位置信息
getLocation().then(res => {
    
    
 // res 就是当前地理位置信息
})

2.使用wx.onLocationChange实时获取地理位置信息

该方法需结合 wx.startLocationUpdateBackground、wx.startLocationUpdate使用

首先在微信公众平台 开发管理 -> 接口设置里面去申请开通该接口
在这里插入图片描述
其次在app.json里面配置

"requiredBackgroundModes": [
   "location"
 ],
 "requiredPrivateInfos": [
   "getLocation",
   "startLocationUpdateBackground",
   "onLocationChange"
 ]

在utils文件下建立location.js再封装成一个方法

/**
 * 切换前后台获取地理位置信息
 * **/
const getLocation = () => {
    
    
  return new Promise((resolve, reject) => {
    
    
    const locationChangeFn = (res) => {
    
    
      resolve(res)
    }

    // 前后台定位
    const startLocationUpdateBackground = () => {
    
    
      wx.startLocationUpdateBackground({
    
    
        success: (res) => {
    
    
          wx.onLocationChange(locationChangeFn)
        }
      })
    }

    // 前台定位
    const startLocationUpdate = () => {
    
    
      wx.startLocationUpdate({
    
    
        success: (res) => {
    
    
          wx.onLocationChange(locationChangeFn)
        },
        fail: (err) => {
    
    
          console.log(err)
        }
      })
    }

    // 授权
    wx.getSetting({
    
    
      success(res) {
    
    
        if (res.authSetting['scope.userLoactionBackground']) {
    
     
          // 前后台定位 
          startLocationUpdateBackground()
        } else if(res.authSetting['scope.userLocation']) {
    
    
          // 前台定位
          startLocationUpdate()
        } else {
    
    
          wx.showModal({
    
    
            content: '需要获取前后台运行定位权限',
            success: (res) => {
    
    
              if (res.confirm) {
    
    
                wx.openSetting({
    
    
                  success: (res) => {
    
    
                    if (res.authSetting['scope.userLocationBackground']) {
    
    
                      // 前后台定位 
                      startLocationUpdateBackground()
                    }else if(res.authSetting['scope.userLocation']) {
    
    
                      // 前台定位
                      startLocationUpdate()
                    } else {
    
    
                      wx.showToast({
    
    
                        title: '不能获取定位',
                      })
                    }
                  },
                  fail: (err) => {
    
    
                    wx.showToast({
    
    
                      title: '不能获取定位',
                    })
                  }
                })
              } else {
    
    
                wx.showToast({
    
    
                  title: '不能获取定位',
                })
              }
            }
          })
        }
      }
    })
  })
}

module.exports = {
    
    
  getLocation
}

在需要调用的地方 引入并调用该方法

import {
    
     getLocation } from '../../utils/location'

getLocation().then(res => {
    
    
  // res 就是实时获取的地理位置
})
也可以这样写(在最近的方法前添加asyncconst {
    
    longitude, latitude} = await getLocation()

使用setinterval在一定的时间内上报地理位置信息
在app.js定义一个全局变量用于在某些条件下清除定位上报

// app.json
App({
    
    
  ......
  globalData: {
    
    
      reportSetInt: null
  }
  ......
})
// 使用地方
const App = getApp()
......
App.globalData.reportSetInt = setInterval(() => {
    
    
  // 上报给后台的方法
  ......
}, 60*1000)
......

猜你喜欢

转载自blog.csdn.net/qq_40121308/article/details/127698305
今日推荐