小程序判断是否授权位置信息和手动授权

news/2024/7/20 1:34:08 标签: 小程序

文章目录

    • 概要
    • 微信小程序的,使用的是高德地图

概要

当用户来到页面之后就会弹出是否授权弹框,但是如果第一次关闭之后,下一次再过来的话页面的授权弹框就不出现了,针对于这种情况写了一个方法

微信小程序的,使用的是高德地图

	getLocationPodel() {
				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) {
					         
					        } else if (res.confirm) {
					         wx.openSetting({
					          success: function (dataAu) {
					           if (dataAu.authSetting["scope.userLocation"] == true) {
					            wx.showToast({
					             title: '授权成功',
					             icon: 'success',
					             duration: 1000
					            })
					            //再次授权,调用getLocationt的API
					            that.getLocation1();
					           } else {
					            wx.showToast({
					             title: '授权失败',
					             icon: 'success',
					             duration: 1000
					            })
					           }
					          }
					         })
					        }
					       }
					      })
					     } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
					      that.getLocation1();
					     }
					     else { //授权后默认加载
					      that.getLocation1();
					     }
					    }
					})
			
			},
			getLocation1() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
						that.getLocation(res.latitude,res.longitude)
					},
					fail:function(res) {
						console.log('dfvdfvdfv')
					}
				});
			},
			getLocation(latitude,longitude) {
				let that = this
				 //根据经纬度获取城市信息
				wx.request({
					url: 'https://apis.map.qq.com/ws/geocoder/v1',
					data: {
						key:'自己的key值,
					    location:`${latitude},${longitude}`
					},
					success: (res) => {
						let address = res.data.result.address_component
						that.city = address.city
						that.province = address.province
					},
					fail: () => {},
					complete: () => {}
				});
			},

http://www.niftyadmin.cn/n/5188821.html

相关文章

EtherCAT从站EEPROM分类附加信息详解:FMMU(现场总线内存管理单元)

0 工具准备 1.EtherCAT从站EEPROM数据(本文使用DE3E-556步进电机驱动器)1 分类附加信息——FMMU(现场总线内存管理单元) 1.1 分类附加信息规范 在EEPROM字64开始的区域存储的是分类附加信息,这里存储了包括设备信息…

CentOS 7上生成HTTPS证书

在CentOS 7上生成HTTPS证书,可以使用OpenSSL工具。以下是在CentOS 7上生成自签名HTTPS证书的步骤: 安装OpenSSL: sudo yum install openssl生成证书和私钥: openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ssl.…

输入框使用正则过滤空格或者特殊字符

文章目录 代码 代码 formatter(value) {var regStr /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\…

c语言数据结构---汉诺塔(普通版+栈)

#include<stdio.h> void move(char a,char c,int n){printf("第%d个盘从%c->%c\n",n,a,c); } void han(char a,char b,char c,int n){if(n1)move(a,c,1);else{han(a,c,b,n-1);move(a,c,n);han(b,a,c,n-1);} } int main(){int n0;printf("总数&#xff…

HTML 超链接 a 标签

在 HTML 标签中&#xff0c;a 标签用于定义超链接&#xff0c;作用是从一个页面链接到另一个页面。 在 a 标签中有两个常用的属性&#xff1a; - href 属性&#xff0c;用于指定链接目标的 url 地址&#xff08;必须属性&#xff09;。当为标签应用 href 属性时&#xff0c;…

最新!11月PMP认证考试地址已公布!

2023年11月25日PMP认证考试即将到来&#xff0c;很多同学都不知道自己在哪里考试&#xff1f;小编将11月25日PMI认证考试才聚各考点地址汇总如下&#xff0c;请各位考生提前查询路线&#xff0c;合理安排出行规划。 11月25日才聚各考点考场地址&#xff1a; 深圳才聚1、2、3、…

一文带你了解ADC测试参数有哪些?

模数转换器&#xff08;ADC&#xff09;是数字电子系统中重要组成部分&#xff0c;用于捕获外部世界的模拟信号&#xff0c;如声音、图像、温度、压力等&#xff0c;并将它们转化为数字信号0\1, 以供计算机进行处理分析。ADC芯片在出厂交付之前&#xff0c;需要对产品的性能做各…

基于SpringBoot的SSMP整合案例(业务层基础开发与快速开发)

业务层基础开发 接口类public interface BookService {boolean save(Book book);boolean update(Book book);boolean delete(Integer id);Book getById(Integer id);List<Book> getAll();IPage<Book> getByPage(int currentPage,int pageSize);IPage<Book> …