微信小程序获取地理位置及经纬度授权代码实例
网络编程 2021-07-04 15:01www.168986.cn编程入门
这篇文章主要介绍了微信小程序获取地理位置及经纬度授权代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了微信小程序获取地理位置及经纬度授权代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
微信小程序获取地理位置授权,首先需要在app.json中添加配置:
"permission": { "scope.userLocation": { "desc": "请确认授权" } }
获取经纬度:如果手机未开启位置信息,那么授权成功后在wx.getLocation()方法中也会一直失败,所以需要在fail方法中提示用户开启手机位置信息
getUserLocation: function () { let vm = this wx.getSetting({ suess: (res) => { // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面 // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权 // res.authSetting['scope.userLocation'] == true 表示 地理位置授权 // 拒绝授权后再次进入重新授权 if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) { // console.log('authSetting:status:拒绝授权后再次进入重新授权', res.authSetting['scope.userLocation']) wx.showModal({ title: '', content: '【泰福利Lite】需要获取你的地理位置,请确认授权', suess: function (res) { if (res.cancel) { wx.showToast({ title: '拒绝授权', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } else if (res.confirm) { wx.openSetting({ suess: function (dataAu) { // console.log('dataAu:suess', dataAu) if (dataAu.authSetting["scope.userLocation"] == true) { //再次授权,调用wx.getLocation的API vm.getLocation(dataAu) } else { wx.showToast({ title: '授权失败', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } } }) } } }) } // 初始化进入,未授权 else if (res.authSetting['scope.userLocation'] == undefined) { // console.log('authSetting:status:初始化进入,未授权', res.authSetting['scope.userLocation']) //调用wx.getLocation的API vm.getLocation(res) } // 已授权 else if (res.authSetting['scope.userLocation']) { // console.log('authSetting:status:已授权', res.authSetting['scope.userLocation']) //调用wx.getLocation的API vm.getLocation(res) } } }) }, // 微信获得经纬度 getLocation: function (userLocation) { let vm = this wx.getLocation({ type: "wgs84", suess: function (res) { // console.log('getLocation:suess', res) var latitude = res.latitude var longitude = res.longitude vm.getDaiShu(latitude, longitude) }, fail: function (res) { // console.log('getLocation:fail', res) if (res.errMsg === 'getLocation:fail:auth denied') { wx.showToast({ title: '拒绝授权', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) return } if (!userLocation || !userLocation.authSetting['scope.userLocation']) { vm.getUserLocation() } else if (userLocation.authSetting['scope.userLocation']) { wx.showModal({ title: '', content: '请在系统设置中打开定位服务', showCancel: false, suess: result => { if (result.confirm) { wx.navigateBack() } } }) } else { wx.showToast({ title: '授权失败', icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1500) } } }) }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程