微信小程序一键获取位置

需求 有个表单需要一键获取对应位置 并显示出来效果如下:

点击一键获取获取对应位置 显示在 picker 默认选中

前端 代码如下:

html 复制代码
     <view class="box_7  {{ showChange==1? 'change-style':'' }}">
          <view class="box_11">
            <view class="text-wrapper_6 {{ showChange==1? 'change-style-postiona':'' }}">
              <text lines="1" class="text_29">考试地址</text>
              <text lines="1" class="text_30" />
              <text lines="1" class="text_31">*</text>
            </view>
                <picker class="region-select" mode="region" bindchange="bindRegionChange" value="{{region}}" custom-item="{{customItem}}">
                <view class="picker">
                {{region[0]}}   {{region[1]}}     {{region[2]}}
                </view>
              </picker>
              <button  bindtap="getUserLocation" class="get-position {{ showChange==1? 'change-style-postion':''}}">一键获取</button>
          </view>
        </view>

一定注意:小程序中要配置,不然获取位置不生效。

javascript 复制代码
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
     }
 },
 "requiredPrivateInfos" : [ "getLocation" ],

小程序js如下:注意 获取到 经纬度之后 请求腾讯 api key 要进行申请 ,申请很快

申请地址 腾讯位置服务 - 立足生态,连接未来

javascript 复制代码
getUserLocation: function () {
  let that = this;
  wx.getSetting({
    success: (res) => {
      if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) 
      {//如果没有授权就提示需要授权
        wx.showModal({
          title: '请求授权当前位置',
          content: '需要获取您的地理位置,请确认授权',
          success: function (res) {
            if (res.cancel) {
              wx.showToast({
                title: '拒绝授权',
                icon: 'none',
                duration: 1000
              })
            } else if (res.confirm) {
              wx.openSetting({
                success: function (dataAu) {
                  if (dataAu.authSetting["scope.userLocation"] == true) {
                    wx.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    //再次授权,调用wx.getLocation的API
                    that.getLocations();
                  } else {
                    wx.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            }
          }
        })
      } else if (res.authSetting['scope.userLocation'] == undefined) {
        that.getLocations();
      }
      else {
      
        that.getLocations();
      }
    }
  })
},
//获取经纬度

getLocations()
{
  let that = this;
  wx.getLocation({
    type: 'wgs84',
    success (res) {
    
      const latitude = res.latitude
      const longitude = res.longitude
      wx.request({
        url: 'https://apis.map.qq.com/ws/geocoder/v1/',
        data: {
          location: `${latitude},${longitude}`,
          key: that.data.key,
          get_poi: 0
        },
        success(res) {
          if(res.statusCode==200){
            const addressComponent = res.data.result.address_component;
            const province = addressComponent.province;
            const city = addressComponent.city;
            const district = addressComponent.district;
            var showChange =0;
            if((province+city+district).length>=12){
              showChange =1;
           }
           var result = [province, city, district];
           that.setData({
             region: result,
             showChange: showChange,
             addressSelect:1,
           })
          }else{
            wx.showToast({
              title: '定位获取失败,请手动选择~',
              icon: 'none'
            })
          }
         
        }
      })

      
    }
   })
},
相关推荐
尚学教辅学习资料8 小时前
基于SpringBoot的图书借阅小程序+LW参考示例
spring boot·后端·小程序·java毕设·图书借阅
luckycoke8 小时前
小程序立体轮播
前端·css·小程序
说私域10 小时前
社群共建与共享:以十点读书会为例探讨开源AI智能名片2+1链动模式S2B2C商城小程序的应用
大数据·人工智能·小程序
初尘屿风10 小时前
小程序类毕业设计选题题目推荐 (29)
spring boot·后端·学习·微信·小程序·课程设计
qq_124987075310 小时前
Java+SpringBoot+数据可视化的家庭记账小程序(程序+论文+安装+调试+售后等)
java·spring boot·小程序·毕业设计
玉阳软件yuyangdev_cn11 小时前
华为IEC104协议对接华为超充小程序
华为·小程序·iec104
feidodo小程序11 小时前
收集信息的表单小程序怎么做_自定义流程表单工具
java·服务器·前端·小程序·微信公众平台
尚学教辅学习资料13 小时前
基于SSM+uniapp的鲜花销售小程序+LW示例参考
小程序·uni-app·java毕设·鲜花销售
web_Hsir15 小时前
vue + uniapp + 高德地图实现微信小程序地图polyline、marker展示
vue.js·微信小程序·uni-app
2401_8854055115 小时前
智能硬件定位技术发展趋势
物联网·微信小程序·uni-app·智慧城市·智能硬件·宠物·智能手表