开发时,uni-app小程序的geLocation接口权限配置

        今天开发小程序,提示用到getLocation,让做配置,一开始以为是要去微信公众平台申请接口权限,申请了几次都没通过,心想开发调试不至于这么麻烦,于是在网上搜索了下答案,果然是可以在manifest.json文件中配置的。应该是要发布的时候才会要申请权限。

一、小程序相关配置
在我们的uni-app小程序项目中首先找到 “manifest.json” 页面,找到 “微信小程序配置” ,点击红色箭头所指位置输入我们自己的小程序APPid,

二、源码视图配置
提示:这里请务必填写,否则无法生效

点击 “源码视图” 找到 “mp-weixin” ,写入以下代码,desc 中的内容可自己编辑

"permission" : {
     "scope.userLocation" : {
          "desc" : "你的位置信息将用于小程序位置接口的效果展示"
     }
 },
"requiredPrivateInfos" : [ "chooseLocation", "getLocation" ]
如图所示:

三、代码使用
提示:这里我们就可以正常使用接口辣

例如:使用 wx.chooseLocation 接口,打开地图,现在相应的位置,这里markers的注意事项参考我曾经写的文章

微信小程序 <map>: marker id should be a number

openMap(){
    var that = this;
    wx.chooseLocation({
        success:function (res) {
            that.markers = [{
                id:1,
                latitude: res.latitude,
                longitude: res.longitude,
                title: res.name,
                iconPath: '/static/jeesite/map/location.png',
                width: 40,
                height:40
            }]
            that.model.appointmentLn = res.latitude;
            that.model.appointmentLo = res.longitude;
            that.model.appointmentAddress = res.address + ' '+ res.name;   // 精准地址加名字
        }
    })
},

再例如:配合腾讯地图SDK,使用 wx.getLocation 获取当前用户的经纬度及地址

getLocation(){
    const that = this;
    wx.getLocation({
        type: 'wgs84',
        success: function (res) {
            console.log(res)
            console.log('当前位置的经度:' + res.longitude);
            console.log('当前位置的纬度:' + res.latitude);
            that.model.visitLo = res.longitude,                        //拜访经度(上门拜访)
            that.model.visitLn = res.latitude
            that.getNowqqMap()
        }
    })
},
 
getNowqqMap(){
    var that = this;
    qqMap.reverseGeocoder({
        //位置坐标,默认获取当前位置,非必须参数 
        //Object格式
        ocation: {
            latitude: that.model.visitLn,
            longitude: that.model.visitLo
        },
        get_poi: 0,        // 是否返回周边POI列表:1.返回;0不返回(默认)
        poi_options: "policy=2;radius=100",
        success: function(res) {//成功后的回调
            that.model.visitAddress = res.result.address
        },
        fail: function(error) {
            console.log('根据经纬度获取精确地址失败');
        },
        complete: function(res) {
                        
        }
    })
},
完结,如有不妥欢迎指出。
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/Mr_LiangDaGe/article/details/134281643

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值