由于wx.getLocation新版有了频率限制,好好的定位接口不能再频繁调用。只能用新版的wx.onLocationChange,但是新版的wx.onLocationChange必须在2.8.1以上才能使用。最后没办法只能做向下兼容
首先判断当前微信的版本号,这里我用了当初判断ios机型的api来获取版本号,存储在全局里面
let capsuleObj = wx.getMenuButtonBoundingClientRect();
wx.getSystemInfo({
success: (res) => {
if(res.platform == "ios" || res.system.indexOf('iOS') > -1 || res.system.indexOf('macOS') > -1){
this.globalData.isIos = true;
}
// console.log("==获取系统信息==");
var statusBarHeight = res.statusBarHeight; //顶部状态栏高度
this.globalData.headerHeight = statusBarHeight + capsuleObj.height + (capsuleObj.top - statusBarHeight) * 2;
let version = res.SDKVersion; // 获取当前基础库版本号
this.globalData.version = version.replace(/\./g, ""); //去除点转换为数字来比较大小
},
fail() {
}
})
然后在获取定位的页面
// 定时获取当前经纬度
getlocation() {
wx.getLocation({
type: 'gcj02',
success: res => {
this.data.signInfo.signLongitude = res.longitude;
this.data.signInfo.signLatitude = res.latitude;
},
fail: error => {
wx.showModal({
title: '提示',
content: '定位失败,请检查是否授权定位',
showCancel: false,
success:res=> {
this.toSetting();
}
})
}
})
if (this.data.localTimer) {
clearInterval(this.data.localTimer)
this.data.localTimer = null;
}
if(app.globalData.version > 2160){
// 如果当前微信版本库大于2.17.0调取新接口
this.data.localTimer = setInterval(() => {
this.getWxLocation() //等待
}, 3000)
}else {
// 低版本调取老接口
this.data.localTimer = setInterval(() => {
wx.getLocation({
type: 'gcj02',
success: res => {
this.data.signInfo.signLongitude = res.longitude;
this.data.signInfo.signLatitude = res.latitude;
},
fail: error => {
}
})
}, 3000)
}
},
// 新版获取定位
getWxLocation() {
let that = this;
try {
let _locationChangeFn = (res) => {
that.data.signInfo.signLongitude = res.longitude;
that.data.signInfo.signLatitude = res.latitude;
wx.offLocationChange(_locationChangeFn) //只监听一次后关闭
}
// 开启小程序进入前台时接收位置消息
wx.startLocationUpdate({
success: (res) => {
// 监听位置信息
wx.onLocationChange(_locationChangeFn)
},
fail: (err) => {
}
})
} catch (error) {
}
},
//设置去开启定位权限
toSetting() {
let self = this
wx.openSetting({
success(res) {
if (res.authSetting["scope.userLocation"]) {
// res.authSetting["scope.userLocation"]为trueb表示用户已同意获得定位信息,此时调用getlocation可以拿到信息
}
}
})
},
最后别忘了在销毁页面时关闭定时器
onUnload: function () {
if (this.data.localTimer) {
clearInterval(this.data.localTimer);
this.data.localTimer = null;
}
},
2023.5.7修改
追加封装方法
getloaction.js
const getLoactionFunc = function(version = 800) {
return new Promise((resolve, reject) => {
if (version > 2160) {
// 如果当前微信版本库大于2.17.0调取新接口
try {
let _locationChangeFn = (res) => {
wx.offLocationChange(_locationChangeFn) //只监听一次后关闭
wx.stopLocationUpdate(_locationChangeFn)
console.log("获取新版定位", res)
resolve(res)
}
// 开启小程序进入前台时接收位置消息
wx.startLocationUpdate({
type: 'gcj02',
success: (res) => {
// 监听位置信息
wx.onLocationChange(_locationChangeFn)
},
fail: (err) => {
}
})
} catch (error) {
reject(error)
}
} else {
// 低版本调取老接口
wx.getLocation({
type: 'gcj02',
success: res => {
console.log("获取老版定位", res)
resolve(res)
},
fail: () => {
reject(error)
}
});
}
})
}
export default getLoactionFunc
只需要需要的页面引用调取就可以了
import getLoactionFunc from "@/utils/getloaction.js"
// 注意传全局获取的微信基础库版本号
getLoactionFunc(this.data.version).then(res=>{
this.data.latitude = res.latitude;
this.data.longitude = res.longitude;
})