小程序获取当前位置
2022/8/3 14:23:52
本文主要是介绍小程序获取当前位置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先先配置小程序位置
在manifest.json文件中,打开源码视图
在源码视图中,找到小程序相关添加上图中蓝色圈起的部分
最后直接上代码
// 获取当前用户的位置 getUserLocation(){ let that = this uni.getSetting({ success: (res) => { let authStatus = res.authSetting['scope.userLocation']; // 如果没有授权位置 if(!authStatus){ //向用户发起授权请求 console.log('授权') uni.authorize({ scope: "scope.userLocation", success: (res) => { // console.log(res) uni.getLocation({ type: 'wgs84', geocode:true,//设置该参数为true可直接获取经纬度及城市信息 success: function (res) { console.log(res) that.reportContent.lat = res.latitude that.reportContent.lon = res.longitude that.reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']' }, fail: function () { uni.showToast({ title: '获取地址失败,请打开手机的定位服务', icon:'none', duration: 1500 }); } }); }, fail: (err) => { console.log(err) uni.showModal({ title: '授权失败', content: '需要获取您当前位置,请在设置界面打开相关权限', success: (res) => { if (res.confirm) { uni.openSetting() } } }) } }) }else{ console.log('已授权') uni.getLocation({ type: 'wgs84', geocode:true,//设置该参数为true可直接获取经纬度及城市信息 success: function (res) { console.log(res) that.reportContent.lat = res.latitude that.reportContent.lon = res.longitude that.reportContent.lonlat = '[' + res.longitude + ',' + res.latitude + ']' }, fail: function () { uni.showToast({ title: '获取地址失败,请打开手机的定位服务', icon:'none' }); } }); } } }) },
这篇关于小程序获取当前位置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30微信小程序的网络设置,及网络请求:wx.request(OBJECT)
- 2024-01-22基于taro搭建小程序多项目框架
- 2024-01-13小程序开发:在插件市场寻找步骤条组件并二开
- 2024-01-05钉钉小程序生态—企业机器人加互动卡片,改善用户体验的开始!
- 2023-12-29【UniApp】-uni-app-打包成小程序
- 2023-12-26性能翻倍!京东亿级体量小程序优化实践
- 2023-12-25小程序优化:第三方SDK过大解决方案
- 2023-11-26微信小程序文件预览和下载-文件系统
- 2023-11-2652天学习微信小程序计划No.2:注册小程序账号&安装开发者工具
- 2023-11-1952天学习微信小程序计划No.1:小程序简介