当前位置:网站首页>封装微信小程序实时获取经纬度+权限判定+引导授权(无30秒延迟)
封装微信小程序实时获取经纬度+权限判定+引导授权(无30秒延迟)
2022-04-22 05:59:00 【周周池】
我们知道微信小程序的wx.getLocationapi可以获取到定位信息(经纬度),但是wx.getLocationapi有个问题,不能实时获取经纬度。wx.getLocationapi在每30秒钟只能有效的获取一次经纬度,比如我坐在高铁或者飞机上面,每隔几秒刷新一次,但是获取到的经纬度是30秒钟之前的经纬度,用户体验会很差。
所以微信小程序还提供了可实时获取经纬度的api,wx.startLocationUpdate,wx.onLocationChange,wx.offLocationChange组合使用
location.js:封装获取经纬度
import $store from '@/store/index.js';
import {
GetLocationType
} from '@/config/AppParameter.js';
/* 请勿随意修改 */
/* 该模块封装获取经纬度方法,支持实时刷新获取最新位置信息 */
const authorization = async () => {
return await GetLocationNo();
};
const GetLocationNo = () => {
//速度快
return new Promise((resolve, reject) => {
uni.getLocation({
type: GetLocationType,
success: function(res) {
let getLocation = {
longitude: res.longitude,
latitude: res.latitude
};
$store.dispatch('man/SUpdateEquipmentInformation_tude', getLocation);
resolve(res);
},
complete: function(error) {
try {
if (error.errMsg.toString().length > 0 && error.errMsg.toString().indexOf(
'wx.onLocationChange') != -1 && $store.getters['man/ReturnVersion'] > 2160) {
getWxLocation()
}
} catch (e) {
}
resolve();
}
});
});
};
const getWxLocation = () => {
//速度慢
return new Promise((resolve, reject) => {
let _locationChangeFn = (res) => {
let getLocation = {
longitude: res.longitude,
latitude: res.latitude
};
$store.dispatch('man/SUpdateEquipmentInformation_tude', getLocation);
resolve(res);
wx.offLocationChange(_locationChangeFn);
};
wx.startLocationUpdate({
type: GetLocationType,
success: (res) => {
wx.onLocationChange(_locationChangeFn);
},
fail: (err) => {
reject();
}
});
});
};
export default authorization;
第一次调用会执行
GetLocationNo函数,GetLocationNo函数通过uni.getLocationapi获取经纬度,当用户30秒内再次执行这个函数时,uni.getLocation会获取失败提示我们可以通过wx.onLocationChange'api实时获取经纬度,那么就调用getWxLocation函数通过组合apiuni.startLocationUpdate,uni.onLocationChange,uni.offLocationChange获取新的经纬度最终获取到经纬度保存到vuex中,再通过Promise的resolve告诉程序取到最新经纬度。
core.js:封装定位授权,判断是否打开定位授权,若没有打开引导用户主动授权
import $store from '@/store/index.js';
import {
ForceLocation
} from '@/config/AppParameter.js';
import authorization from '../common/location.js';
/* 请勿随意修改 */
/* 封装授权权限判断,按需弹出授权框 */
const GetLatitudeAndlongitude = function() {
return new Promise((resolve, reject) => {
uni.getSystemInfo({
success(res) {
let locationEnabled = res.locationEnabled;
let locationAuthorized = res.locationAuthorized;
if (locationEnabled == false) {
if($store.getters['man/ReturnOnOffEmpowerLocation']){
uni.hideLoading()
uni.showModal({
title: '提示',
content: '检测到您手机定位服务(GPS)未授权,请去完善一下哦~',
success: function(res) {
if(res.confirm){
clearlocation();
resolve();
}else if(res.cancel){
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
resolve();
}
}
});
}else{
resolve();
}
} else if (locationAuthorized == false) {
if($store.getters['man/ReturnOnOffEmpowerLocation']){
uni.hideLoading()
uni.showModal({
title: '提示',
content: '检测到微信禁止获取定位服务,请去完善一下哦~',
success: function(res) {
if(res.confirm){
clearlocation();
resolve();
}else if(res.cancel){
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
resolve();
}
}
});
}else{
resolve();
}
} else {
uni.getSetting({
success: (res) => {
if (!res.authSetting['scope.userLocation']) {
/* 未授权 */
uni.authorize({
scope: 'scope.userLocation',
success: (res) => {
authorization().then(res => {
$store.dispatch('man/SUpdataHomnepageRefresh',true);
resolve();
});
},
fail: (error) => {
if($store.getters['man/ReturnOnOffEmpowerLocation']){
uni.hideLoading()
uni.showModal({
title: '授权提示',
content: '请求获取您的位置信息,才能开启相关功能哦~',
showCancel: !
ForceLocation,
confirmText: '去授权',
success: (res) => {
if (res.confirm) {
$store.dispatch('man/SUpOnOffEmpowerLocation', true);
uni.openSetting({
success: (res) => {
uni.getSetting({
success:(trRes)=>{
if(trRes.authSetting['scope.userLocation']){
$store.dispatch('man/SUpdataHomnepageRefresh',true);
authorization().then(res => {
resolve();
});
}else{
$store.dispatch('man/SUpdataHomnepageRefresh',false);
clearlocation();
resolve();
}
}
})
},
});
} else if (res.cancel) {
//取消授权
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
uni.showToast({
title: '获取位置信息失败,请开启定位权限',
duration: 3500,
icon: 'none'
});
clearlocation();
resolve();
}
}
});
}else{
resolve();
}
},
});
} else {
/* 已授权 */
authorization().then(res => {
// $store.dispatch('man/SUpdataHomnepageRefresh',true);
resolve();
});
}
},
fail: (Error) => {
console.error('获取用户位置信息失败', Error);
}
});
}
},
fail() {
if($store.getters['man/ReturnOnOffEmpowerLocation']){
uni.hideLoading()
uni.showModal({
title: '提示',
content: '检测到您手机定位服务(GPS)未授权,请去完善一下哦~',
success: function(res) {
if(res.confirm){
clearlocation();
resolve();
}else if(res.cancel){
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
resolve();
}
}
});
}else{
resolve();
}
}
});
});
};
/* 清除定位信息 */
const clearlocation = () => {
let StoreDataList = $store.getters['man/ReturnStoreDataList']
Object.assign(StoreDataList,{
distance:null})
$store.dispatch('man/SUpdataStoreDataList',StoreDataList)
$store.dispatch('man/SUpdateEquipmentInformation_tude', {
longitude: '',latitude: ''});
};
export {
GetLatitudeAndlongitude
};
版权声明
本文为[周周池]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45444562/article/details/124294210
边栏推荐
- 一套sql语句同时支持Oracle跟Mysql?
- 剑指offer:对称的二叉树(递归 迭代leetcode 101)
- Iframe child parent pass parameter
- EXCEL 分列功能的使用
- 小程序自定义原生底部导航
- A set of SQL statements supports both Oracle and MySQL?
- Automatically add partitions for PostgreSQL tables
- 从零开始学安卓(kotlin)一 ——入门
- Pixel 5 5G解锁教程(含解锁BL,安装EdXposed与Root)
- webService接口编写并发布与webService接口的调用(二)
猜你喜欢

指纹支付相关的细节处理

EXCEL IFS函数的使用

Shumei technology was selected into the top 20 of Chaoyang high tech high growth in 2021

Shumei technology and surging news jointly released the "network information content security insight report"

EXCEL IF、AND以及OR函数的嵌套使用

ArcGIS 观景点视域分析

Pgdoucer best practices: Series 4

点击触发其他dom元素:< $refs,$el >

TCP和UDP区别详解

在微信小程序中打开的页面不能超过10个,达到10个页面后,就不能再打开新的页面
随机推荐
Redis fetches out data with garbled code
Flink源码之StreamExecutionEnvironment
一个三目表达式,引起的空指针
《信息系统项目管理师总结》第一章 项目整体管理
A set of SQL statements supports both Oracle and MySQL?
关于调试指纹时候遇到的其他问题
EXCEL IFS函数的使用
Uniapp global interception 401 jumps to the login page
kubectl命令自动补齐
创新实训(六)路由
js,jq单行文字上下滚动
pixel手机救砖教程
Digital beauty technology won the "top ten intelligent risk control management innovation award" of the banker
使用kubeadm安装kuberneters
Pixel系列无痛升级
The digital risk control summit of digital America 2022 was opened, and the five highlights were exposed in advance
uglifyjs压缩JS
Shumei technology was invited to participate in the content governance label seminar of ICT Institute
Open source database management systems are now more popular than commercial products
Cancel password after excel worksheet forgets password