uniapp开发微信小程序,选择地理位置uni.chooseLocation

devtools/2024/9/23 17:26:52/
<view @click="toCommunity">点击选择位置</view>
    toCommunity() {const that = thisuni.getSetting({success: (res) => {const status = res.authSetting// 如果当前设置是:不允许,则需要弹框提醒客户,需要前往设置页面打开授权if(!status['scope.userLocation']) {uni.showModal({title: "是否授权当前位置",content: "需要获取您的地理位置,请确认授权,否则地图功能将无法使用",success: (tip) => {if (tip.confirm) {// 如果已经拒绝过,则需要打开设置页面,授权弹框不会弹出第二次,因为已经明确拒绝/确认过了(微信的原因)if (that.isDeny) {wx.openSetting({success: function(res) {// 在设置页面授权成功后再次获取位置信息uni.showToast({title: "授权成功",})that.isDeny = false// 修改授权后返回页面,弹框消失,需要再点一次},fail: (data) => {console.log(data)// isDeny 是否拒绝过授权,如果拒绝过,再点击按钮的话,弹框确认后就直接打开小程序>微信小程序设置页that.isDeny = true}})return}// 如果点击了确认,并且没有拒绝过微信系统授权弹框,则会弹出授权位置信息的弹框uni.authorize({scope: "scope.userLocation",success: (data) => {// 授权弹框点击确认console.log(data)// 如果用户同意授权,则打开授权设置页面,判断用户的操作uni.openSetting({success: (data) => {// 如果用户授权了地理信息在,则提示授权成功if (data.authSetting['scope.userLocation'] === true) {uni.showToast({title: "授权成功",icon: "none",duration: 1000})}}})},fail: (data) => {// 如果用户拒绝授权,则提示用户需要授权uni.showToast({title: "您已拒绝授权,请重新授权",icon: "none",duration: 1000})that.isDeny = true}})}}})} else {uni.chooseLocation({success: (res) => {that.formData.village = res.namethat.formData.address = res.addressthat.formData.longitude = res.longitudethat.formData.latitude = res.latitudeconst map = new amapFile.AMapWX({key: 'f037f0a9966f01339818bbe2ec1c6495',})map.getRegeo({location: res.longitude + ',' + res.latitude,success: (data) => {this.formData.sheng = data[0].regeocodeData.addressComponent.provincethis.formData.shi = data[0].regeocodeData.addressComponent.citythis.formData.qu = data[0].regeocodeData.addressComponent.district},})},fail: () => {}})}},fail: () => {that.isDeny = true},})},

在这里插入图片描述

下方列表可以选择地址,选择后点击右上角完成,会返回页面

在这里插入图片描述

    toCommunity() {uni.getSetting({success: function(res) {if (!res.authSetting['scope.userLocation']) { // 如果没有授权定位if (that.isFirstTime) { // 如果是第一次尝试// 弹出提示框询问用户是否授权uni.showModal({title: '提示',content: '需要获取您的地理位置信息',success: function(modalRes) {if (modalRes.confirm) {// 用户点击了确认,尝试请求授权uni.authorize({scope: 'scope.userLocation',success: function() {// 授权成功,调用openMap方法that.openMap()},fail: function() {// 授权失败,可能是用户拒绝了,此时可以打开设置页面that.openSetting()}})}}})that.$store.commit('updateIsFirstTime', false) // 更新为已尝试授权} else {// 用户之前已经拒绝过授权,直接打开设置页面that.openSetting()}} else {// 已经授权,直接调用openMap方法that.openMap()}},fail: function(err) {// 获取设置失败的处理console.error('获取用户设置失败:', err)}})},openSetting() {// 打开设置页面uni.openSetting({success: function(res) {if (res.authSetting['scope.userLocation']) {// 用户在设置页面打开了定位权限this.openMap()}},fail: function(err) {// 打开设置页面失败的处理console.error('打开设置页面失败:', err)}})},openMap() {uni.chooseLocation({success: (res) => {console.log('用户选择的地址:', res)this.formData.village = res.namethis.formData.address = res.addressthis.formData.longitude = res.longitudethis.formData.latitude = res.latitudeconst map = new amapFile.AMapWX({key: 'f037f0a9966f01339818bbe2ec1c6495',})map.getRegeo({location: res.longitude + ',' + res.latitude,success: (data) => {this.formData.sheng = data[0].regeocodeData.addressComponent.provincethis.formData.shi = data[0].regeocodeData.addressComponent.citythis.formData.qu = data[0].regeocodeData.addressComponent.district},})},fail: (err) => {console.log('选择位置失败:', err)}})},

http://www.ppmy.cn/devtools/39507.html

相关文章

ios实现拍摄视频与显示在界面上

1、添加录音和拍摄权限 NSMicrophoneUsageDescription Privacy - Camera Usage Description 2、代码 #import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <MobileCoreServices/MobileCoreServices.h>// 接下来是你的 ViewCont…

部署达梦数据库主从配置详细操作DM8

服务器配置 主库 192.168.81.128 实例名 dm-1 从库 192.168.81.129 实例名 dm-2 以下安装部署主从服务器都操作 关闭防火墙 systemctl stop firewalld && systemctl disable firewalld 注意安装前必须创建 dmdba 用户&#xff0c;禁止使用 root 用户安装数据库。…

【QA】Java集合常用的函数

文章目录 前言Collection接口通用函数 | Collections工具类通用函数 | List接口 Set接口List接口ArrayListLinkedList Set接口TreeSetHashSetLinkedHashSet Map接口通用函数TreeMapHashMapLinkedHashMap 前言 本文介绍Java集合中常用的函数。 Collection接口 通用函数 | Co…

普通组件的注册-局部注册和全局注册

目录 一、局部注册和全局注册-概述 二、局部注册的使用示例 三、全局注册的使用示例 一、局部注册和全局注册-概述 组件注册有两种方式&#xff1a; 局部注册&#xff1a;只能在注册的组件内使用。使用方法&#xff1a;创建.vue文件&#xff0c;在使用的组件内导入并注册。…

Linux常见指令2️⃣

目录 cp指令&#xff08;重要&#xff09; mv指令&#xff08;重要&#xff09; cat、tac head、tail指令&#xff08;重要&#xff09; 知识点 时间相关的指令 知识点&#xff1a; Cal指令 grep 指令 zip/unzip指令 知识点 cp指令&#xff08;重要&#xff09; 语法…

使用Express+Node.js搭建网站

Express是一个基于Node.js平台的快速、开放、极简的Web开发框架。它的作用是专门用来创建Web服务器&#xff0c;与Node.js内置的http模块功能相似&#xff0c;但更为简便和高效。 Express中文官网&#xff1a;Express - 基于 Node.js 平台的 web 应用开发框架 - Express中文文…

MySQL数据库及数据表的创建

1.创建一个名叫 db_classes 的数据库&#xff1a; 创建一个叫 db_classes 的数据库MySQL命令&#xff1a; create database db_classes; 运行效果&#xff1a; 创建数据库后查看该数据库基本信息MySQL命令&#xff1a; show create database db_classes; 运行效果&#xff…

【半夜学习MySQL】库的操作(含库的创建、删除、修改、备份操作/查看mysql连接情况/字符集和校验规则详谈)

&#x1f3e0;关于专栏&#xff1a;半夜学习MySQL专栏用于记录MySQL数据相关内容。 &#x1f3af;每天努力一点点&#xff0c;技术变化看得见 文章目录 创建数据库字符集和校验规则查看字符集合校验规则校验规则对数据库的影响 操纵数据库数据备份和恢复查看连接情况 创建数据库…