// index.js
// 获取应用实例
const app = getApp()
Page({
data: {},
onLoad() {},
mapSetting: function () {
let that = this;
// 获取用户当前设置
wx.getSetting({
success: (res) => {
// 微信授权拒绝之后无法再次调用起授权弹框,所以可以通过调用设置权限按钮来打开权限信息
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
wx.showModal({
title: '',
content: '我们需要获取你的地理位置,请确认授权',
success: function (res) {
if (res.cancel) {
wx.showToast({
title: '您已拒绝授权',
icon: 'none'
})
} else if (res.confirm) {
// 调起客户端小程序设置界面,返回用户设置的操作结果
wx.openSetting({
success: function (dataAu) {
if (dataAu.authSetting["scope.userLocation"] == true) {
that.getLocation(dataAu);
} else {
wx.showToast({
title: '授权失败',
icon: 'none'
})
}
}
})
}
}
})
}
// 初始化进入,未授权
else if (res.authSetting['scope.userLocation'] == undefined) {
that.getLocation(res);
}
// 已授权
else if (res.authSetting['scope.userLocation']) {
that.getLocation(res);
}
}
});
},
/**
* 获取地理信息
*/
getLocation: function (userLocation) {
let that = this;
wx.getLocation({
// gcj02:返回国测局坐标,即经纬度加密后的坐标。
type: "gcj02",
success: function (res) {
let latitude = res.latitude;
let longitude = res.longitude;
console.log(res);
},
fail: function (res) {
if (res.errMsg === 'getLocation:fail:auth denied') {
wx.showToast({
title: '您已拒绝授权',
icon: 'none'
})
return
}
// 使用可选链简化代码并避免在访问不存在的属性时出现错误
if (!userLocation?.authSetting?.['scope.userLocation']) {
that.mapSetting();
} else if (userLocation.authSetting?.['scope.userLocation']) {
wx.showModal({
title: '',
content: '请在系统设置中打开定位服务',
showCancel: false,
success: result => {
}
})
} else {
wx.showToast({
title: '授权失败',
icon: 'none'
})
}
}
});
},
})
没有合适的资源?快使用搜索试试~ 我知道了~
微信小程序定位模块或者访问权限拒绝一般要如何处理
共16个文件
json:6个
js:5个
wxss:3个
需积分: 5 0 下载量 86 浏览量
2023-06-12
17:10:10
上传
评论
收藏 7KB ZIP 举报
温馨提示
微信小程序定位模块或者访问权限拒绝一般要如何处理
资源推荐
资源详情
资源评论
收起资源包目录
location.zip (16个子文件)
sitemap.json 191B
project.private.config.json 336B
pages
index
index.wxml 131B
index.js 3KB
index.json 27B
index.wxss 0B
logs
logs.json 77B
logs.js 305B
logs.wxml 197B
logs.wxss 106B
.eslintrc.js 587B
app.json 444B
project.config.json 1KB
app.js 364B
utils
util.js 460B
app.wxss 194B
共 16 条
- 1
资源评论
An_Adorable_Dali
- 粉丝: 201
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功