ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

小程序获取当前位置

2022-08-02 19:05:08  阅读:166  来源: 互联网

标签:console log success res 位置 程序 获取 uni reportContent


首先先配置小程序位置

在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'
                                    });
                                }
                            });
                        }
                    }
                })
                
            },

 

标签:console,log,success,res,位置,程序,获取,uni,reportContent
来源: https://www.cnblogs.com/wq805/p/16544818.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有