当前位置: 首页 > news >正文

微信小程序一键获取位置

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

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

前端  代码如下:

     <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>

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

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

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

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

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'
            })
          }
         
        }
      })

      
    }
   })
},

相关文章:

  • 解决windows10、windows11故障:Microsoft-Windows-Kernel-Processor-Power 事件ID:37
  • Could not find artifact com.sleepycat;je:jar:7.3.7 in aliyunmaven
  • 智能水印相机微信小程序源码
  • 深度学习中常用的激活函数有sigmoid、tanh、ReLU、LeakyReLU、PReLU、GELU等。
  • 08-React扩展
  • c/c++的include机制简述
  • STM32不使用 cubeMX实现外部中断
  • 全网最丑焊锡教程(仅排针焊接心得)
  • 【C++代码】二叉搜索树的最近公共祖先,二叉搜索树中的插入操作,删除二叉搜索树中的节点--代码随想录
  • react native 使用夜神模拟器开发调试 windows+android
  • Scala语言入门
  • Linux进阶-ipc共享内存
  • jdk9模块化
  • 深度学习学习笔记-模型的修改和CRUD
  • Spring IOC之@ComponentScan
  • LAXCUS分布式操作系统是怎么实现的?
  • 【广州华锐互动】利用AR进行野外地质调查学习,培养学生实践能力
  • 【算法教程】排列与组合的实现
  • 华为OD 绘图机器(100分)【java】A卷+B卷
  • 项目经理之识别项目干系人
  • 解放日报:让算力像“水电煤”赋能千行百业
  • 马上评|扩大高速免费救援范围,打消出行后顾之忧
  • 空调+零食助顶级赛马备战,上海环球马术冠军赛即将焕新登场
  • 神十九飞船已撤离空间站,计划于今日中午返回东风着陆场
  • 民生银行一季度净利127.42亿降逾5%,营收增7.41%
  • 中使馆:奉劝菲方有关人士不要在台湾问题上挑衅,玩火者必自焚