微信小程序 监听位置信息
wx.onLocationChange(function callback) | 微信開放文檔微信開發(fā)者平臺(tái)文檔https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.onLocationChange.html
小程序 獲取當(dāng)前城市位置-高德地圖_Start2019-CSDN博客小程序獲取位置信息,包括省市區(qū)、用戶拒絕后,調(diào)起用戶授權(quán)設(shè)置頁,重新授權(quán)獲取位置信息https://blog.csdn.net/Start2019/article/details/122542917上一篇文章是獲取用戶所在城市地址信息,這里是根據(jù)經(jīng)緯度監(jiān)聽位置,大概3秒刷新一次經(jīng)緯度。
需要在app.json中配置
"requiredBackgroundModes": ["location"],"permission": {"scope.userLocation": {"desc": "你的位置信息將用于小程序掃描貨物碼時(shí)的地址展示"}},微信小程序后臺(tái)持續(xù)定位功能使用 - ShawYoi - 博客園微信小程序團(tuán)隊(duì)在7月30日更新了 基礎(chǔ)庫 2.8.0 其中新添加了小程序后臺(tái)持續(xù)定位功能和聯(lián)系定位的接口 從上到下分別是 1.wx.onLocationChange//監(jiān)聽位置實(shí)時(shí)變化 2.wx.sthttps://www.cnblogs.com/cokolxvd/p/11393510.html
具體做法:?
const app = getApp(); Page({onShow: function () { this.wxLocation(); //檢測(cè)是否授權(quán)位置信息,若授權(quán)則開始監(jiān)聽位置//開啟監(jiān)聽const _locationChangeFn = res=> {// console.log('location change', res.latitude, res.longitude);app.getAddress(res.latitude, res.longitude)}wx.onLocationChange(_locationChangeFn); },//生命周期函數(shù)--監(jiān)聽頁面隱藏onHide: function () {wx.offLocationChange(); //取消監(jiān)聽},//檢測(cè)位置信息wxLocation(){wx.getSetting({success(res){ if(res.authSetting['scope.userLocationBackground']){wx.startLocationUpdate({success: (res) => {console.log('startLocationUpdate-res', res)},fail: (err) => {console.log('startLocationUpdate-err', err)}})} else {if(!res.authSetting['scope.userLocation']){ //打開設(shè)置頁面去授權(quán)//掃描貨物碼時(shí)開始提示。否則取消后還是能繼續(xù)掃描,只有返回到該頁面時(shí)才提示,起不到引導(dǎo)用戶授權(quán)效果 } else { wx.startLocationUpdate({success: (res) => {console.log('startLocationUpdate-res', res)},fail: (err) => {console.log('startLocationUpdate-err', err)}}) }} //判斷authSetting['scope.userLocationBackground']結(jié)束}});},//點(diǎn)擊貨物碼click: function(){wx.getSetting({success(res){if(!res.authSetting['scope.userLocation']){app.utils.showModal('檢測(cè)到您沒打開此小程序的定位權(quán)限,是否去設(shè)置打開?', '提示').then(()=>{wx.openSetting({success: function(e){console.log("打開授權(quán)頁面",e);app.getUserLocation(); //獲取地址},})})} else {//已授權(quán)要做的操作}}});} })app.getAddress(res.latitude, res.longitude) 是我封裝在 app.js 中的函數(shù),具體使用過程可以在上個(gè)博客中去看一下。
const amapFile = require('./libs/amap-wx.130'); App({//獲取用戶經(jīng)緯度 latitude緯度, longitude經(jīng)度getUserLocation(){var that = this;wx.getLocation({success: function(res){console.log("經(jīng)緯度",res); // that.setData({"userInfo.latitude": res.latitude, "userInfo.longitude": res.longitude});that.getAddress(res.latitude,res.longitude)}})},//轉(zhuǎn)換成省市區(qū) latitude緯度,long經(jīng)度getAddress(latitude, longitude){// latitude="22.26",longitude = "112.57";var that = this;var myAmapFun = new amapFile.AMapWX({ key: that.addressKey }); myAmapFun.getRegeo({location: '' + longitude + ',' + latitude + '',//location的格式為'經(jīng)度,緯度'success: function (data) {let {province,city,district} = data[0].regeocodeData.addressComponent;city = (city || city.length>0) ? city:"";console.log("省市區(qū)", province,city,district)},fail: function (info) { }})}, })微信小程序?qū)崟r(shí)獲取用戶經(jīng)緯度地理位置信息_電腦小技巧_上網(wǎng)技巧_QQ地帶微信小程序?qū)崟r(shí)獲取用戶經(jīng)緯度地理位置信息https://www.oicqzone.com/pc/2019092024670.html
總結(jié)
以上是生活随笔為你收集整理的微信小程序 监听位置信息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序 获取手机号
- 下一篇: layui 父页面弹框中获取子页面的内容