微信怎么一直开实时定位(微信怎么一直使用定位)
微信怎么一直开实时定位(微信怎么一直使用定位)
微信小程序团队在7月30日更新了 基础库 2.8.0
其中新添加了小程序后台持续定位功能和联系定位的接口
从上到下分别是
1.wx.onLocationChange//监听位置实时变化
2.wx.stopLocationUpdate//关闭监听实时位置变化,前后台都停止消息接收
3.wx.startLocationUpdate//开启小程序进入前台时接收位置消息
4.wx.startLocationUpdataBackground//开启小程序进入前后台时均接收位置消息
【微信怎么一直开实时定位(微信怎么一直使用定位)】详细信息可查看
https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.startLocationUpdateBackground.html
刚好正在做的项目需要用到后台的定位,下面介绍下使用方法:
要实现后台持续定位需要用到上述1,2,4
1.首先需要在app.josn中加入,这样小程序才能在后台调用定位功能
"requiredBackgroundModes": [ "location"],2.在页面index.js中加入我们所需要用到的api
首先我们需要开启小程序进入前后台时均接收位置消息
wx.startLocationUpdateBackground({ success(res) { console.log('开启后台定位', res) }, fail(res) { console.log('开启后台定位失败', res) } })注意这个API无法在开发者工具上调试,只能用真机来进行
在调用完这个方法成功后我们便可以使用wx.onLocationChange来获取实时的位置变化了
wx.onLocationChange(function(res) { console.log('location change', res) })下图为真机调试获取到的实时定位信息
大概每3秒钟会获取一次新的定位信息,小程序进入后台之后状态栏会显示小程序正在使用位置信息,如下图
如何想要关闭需要调用wx.stopLocationUpdate方法
经过测试在调用完wx.stopLocationUpdate停止监听实时位置变化后
再次调用
wx.startLocationUpdataBackground后wx.onLocationChange无需重新调用也会继续运作
- 晒生活还是隐私 未成年人短视频存隐患该怎么管?
- 小红书被曝推送未成年人身体隐私 怎么管?
- 好险!国际空间站为躲垃圾急变轨 太空怎么搞卫生
- 北京地铁乞讨乱象是怎么破解的?背后有她的努力
- 刘国梁揭秘:中美选手跨国搭档参赛是怎么促成的?
- 如何辨别正规外汇行为和地下钱庄?听国家外汇局怎么说
- 《失孤》原型团圆背后:警方是怎么找到嫌犯的?
- 袁雨萱道歉佟大为是怎么回事 袁雨萱佟大为发生了什么事
- 殷世航和韩安冉的关系怎么样 连麦是什么时候
- 一年一度喜剧大赛金靖怎么复活了 金靖淘汰是哪一期