当前位置:网站首页>Encapsulate wechat applet to obtain longitude and latitude + permission determination + guidance authorization in real time (no 30 second delay)
Encapsulate wechat applet to obtain longitude and latitude + permission determination + guidance authorization in real time (no 30 second delay)
2022-04-22 06:44:00 【Zhou zhouchi】
We know about wechat applet wx.getLocationapi You can get location information ( Longitude and latitude ), however wx.getLocationapi There is a problem , Unable to obtain longitude and latitude in real time .wx.getLocationapi In each of the 30 You can only effectively obtain longitude and latitude once per second , For example, I sit on the high-speed railway or plane , Refresh every few seconds , But the latitude and longitude obtained is 30 The latitude and longitude seconds ago , The user experience will be very poor .
Therefore, wechat applet also provides a way to obtain longitude and latitude in real time api,wx.startLocationUpdate,wx.onLocationChange,wx.offLocationChange Use a combination of
location.js: Package to obtain longitude and latitude
import $store from '@/store/index.js';
import {
GetLocationType
} from '@/config/AppParameter.js';
/* Please do not modify it at will */
/* The module encapsulates the method of obtaining longitude and latitude , Support real-time refresh to obtain the latest location information */
const authorization = async () => {
return await GetLocationNo();
};
const GetLocationNo = () => {
// Fast
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 = () => {
// Slow speed
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;
The first call will execute
GetLocationNofunction ,GetLocationNoFunction byuni.getLocationapi Get latitude and longitude , When the user 30 When this function is executed again within seconds ,uni.getLocationWill get the failure prompt. We can usewx.onLocationChange'api Real time acquisition of latitude and longitude , So callgetWxLocationFunctions by combining apiuni.startLocationUpdate,uni.onLocationChange,uni.offLocationChangeGet the new latitude and longitude, and finally save the latitude and longitude tovuex in, Re passPromiseOfresolveTell the program to get the latest longitude and latitude .
core.js: Packaging location authorization , Determine whether to open the location authorization , If it is not opened, guide the user to actively authorize
import $store from '@/store/index.js';
import {
ForceLocation
} from '@/config/AppParameter.js';
import authorization from '../common/location.js';
/* Please do not modify it at will */
/* Encapsulate authorization authority judgment , The authorization box pops up on demand */
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: ' Tips ',
content: ' Your mobile location service has been detected (GPS) unauthorized , Please perfect it ~',
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: ' Tips ',
content: ' It is detected that wechat prohibits obtaining location services , Please perfect it ~',
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']) {
/* unauthorized */
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: ' Authorization tips ',
content: ' Request your location information , To enable related functions ~',
showCancel: !
ForceLocation,
confirmText: ' Delegating ',
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) {
// Cancel Authorization
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
uni.showToast({
title: ' Failed to get location information , Please open the location permission ',
duration: 3500,
icon: 'none'
});
clearlocation();
resolve();
}
}
});
}else{
resolve();
}
},
});
} else {
/* Authorized */
authorization().then(res => {
// $store.dispatch('man/SUpdataHomnepageRefresh',true);
resolve();
});
}
},
fail: (Error) => {
console.error(' Failed to get user location information ', Error);
}
});
}
},
fail() {
if($store.getters['man/ReturnOnOffEmpowerLocation']){
uni.hideLoading()
uni.showModal({
title: ' Tips ',
content: ' Your mobile location service has been detected (GPS) unauthorized , Please perfect it ~',
success: function(res) {
if(res.confirm){
clearlocation();
resolve();
}else if(res.cancel){
$store.dispatch('man/SUpOnOffEmpowerLocation', false);
resolve();
}
}
});
}else{
resolve();
}
}
});
});
};
/* Clear location information */
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
};
版权声明
本文为[Zhou zhouchi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220557472070.html
边栏推荐
猜你喜欢

小程序自定义原生底部导航

一套sql语句同时支持Oracle跟Mysql?

EXCEL 利用替换、分列、填充功能综合整理财务数据

The app enters for the first time and pops up the service agreement and privacy policy

ArcGIS 纵断面分析

关于一段小程序的思考

自定义微信小程序顶部导航栏(自适应微信胶囊按钮,flex布局)

Uniapp generates Canvas Product posters

剑指offer:数据流中的中位数(优先队列 大顶堆小顶堆 leetcode 295)

COM in Wine(2)——基本代码分析
随机推荐
比properties更好用的读配置文件的方式
To summarize the timers I've used: @ scheduled annotation
Redis的安装启动,菜鸟使用(windows)
总结一下我使用过的定时器:@Scheduled注解
JS five methods to judge the most complete data types
一套sql语句同时支持Oracle跟Mysql?
ApplicationContext注入Bean
uniapp解决首页点击返回(手机返回键)跳到登录页
windows远程连接redis
ArcGIS 观景点视域分析
Regular verification
手写通用防抖与节流函数
Uniapp generates Canvas Product posters
点击触发其他dom元素:< $refs,$el >
Intersection of interval lists
nodejs+Express+mongodb
redis存入数据显示乱码问题
滚动条的多种样式
树莓派+opencv+opencv--人脸检测------------环境搭建
Pixel 5 5G解锁教程(含解锁BL,安装EdXposed与Root)