微信小程序-wx.authorize-wx.getSetting-wx.openSetting-wx.chooseLocation

微信小程序-位置授权-2020-2-14

1、app.json

"permission": {
 "scope.userLocation": {
  "desc": "你的位置信息将用于小程序位置接口
 }
 }

2、.js

 onShow: function () {
//初始获取定位权限
wx.authorize({
 scope: 'scope.userLocation',
 success: (res) => {
 
 },
})
},
getLocation:function(e){
var that = this
wx.getSetting({
 success(res){
 //这里判断是否有地位权限
  if (!res.authSetting['scope.userLocation']) {
  wx.showModal({
   title: '提示',
   content: '请求获取位置权限',
   success:function(res){
   if(res.confirm==false){
    return false;
   }
   wx.openSetting({
    success(res) {
    //如果再次拒绝则返回页面并提示
    if (!res.authSetting['scope.userLocation']) {
     wx.showToast({
     title: '此功能需获取位置信息,请重新设置',
     duration: 3000,
     icon: 'none'
     })
    } else {
     //允许授权,调用地图
     that.chooseMap()
    }
    }
   })
   }
  }) 
  } else {
  //如果有定位权限,调用地图
  that.chooseMap()
  }
 
 }
 
})
},
 
chooseMap(){
var that = this
wx.chooseLocation({
 success: function (res) {
 that.setData({
  address: res.address,
  latitude: res.latitude,
  longitude: res.longitude
 })
 },
 fail: function (res) {
 console.log(res)
 }
})
},

3、.wxml

<button class="pre" size="mini" type="default" bindtap='getLocation'>选择位置</button>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值