微信小程序一键获取位置

news/2024/7/20 2:03:55 标签: 微信小程序, 小程序

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

点击一键获取获取对应位置 显示在  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'
            })
          }
         
        }
      })

      
    }
   })
},


http://www.niftyadmin.cn/n/5100858.html

相关文章

C#开发的OpenRA游戏之金钱系统(3)

C#开发的OpenRA游戏之金钱系统(3) 前面已经说到通过类FindAndDeliverResources来实现找到矿场,以及最近的精炼工厂,这样采矿车才能源源不断地从矿场采集大量的资源,运往工厂,才能换回来大量的金钱,这样才能维持整个游戏的开销。 类FindAndDeliverResources里是怎么样实…

同城代驾开源版小程序开发

同城代驾开源版小程序开发 功能特性描述&#xff1a; 定价模式&#xff1a;本系统支持灵活的计价模式&#xff0c;包括白天和夜晚的起步价、起步里程、每公里价以及超时费用&#xff0c;从而满足不同时段的定价需求。 实时路径计算&#xff1a;通过集成腾讯地图的软件开发工…

水浸监测新方法,简单实用,你值得拥有!

随着科技的不断进步&#xff0c;监控系统已成为我们生活中不可或缺的一部分。然而&#xff0c;许多监控系统在面临水浸等自然灾害时显得无能为力&#xff0c;这可能导致严重的损失和危险。 因此&#xff0c;水浸监控系统的需求不断增加&#xff0c;以保护我们的财产和安全。 客…

C++基本语法【恩培学习笔记(一)】

文章目录 1、C程序结构1.1 C程序的基本组成部分1.2 预处理指令1.3 注释1.4 main() 主函数1.5 命名空间 namespace 2、 C的变量和常量2.1 变量2.2 变量的声明2.3 变量的类型 3、C 数组和容器3.1 数组&#xff08;array&#xff09;3.2 容器&#xff08;vector&#xff09; 4、C …

【esp32】arduino-数码管

一、数码管类型 按发光二极管单元连接方式分为共阳型数码管和共阴型数码管&#xff0c; 区分:1、可通过查看数码管引脚类型 2、观察数码管点亮状态 3、查询数码管规格表 二、共阳型数码管 是指将所有发光二极管的阳极接到一起形成公共阳极&#xff08;COM)的数码管 三、共…

中国人民大学与加拿大女王大学金融硕士项目:开启你的金融精英之路

在全球化的今天&#xff0c;金融行业的发展日新月异&#xff0c;对金融人才的需求也日益增长。为了满足这一需求&#xff0c;中国人民大学与加拿大女王大学联合推出了金融硕士项目&#xff0c;旨在培养具有国际视野、专业素养和创新能力的金融精英。 这一开创性的项目将两大世…

【Loopback Detection 环回检测以及原理解读】

Loopback Detection简介 Loopback Detection&#xff08;环回检测&#xff09;通过周期性发送环回检测报文来检测设备下挂网络是否存在环路。 网络中的环路会导致设备对广播、组播以及未知单播等报文进行重复发送&#xff0c;造成网络资源浪费甚至网络瘫痪。为了能够及时发现…

从十月稻田,看大米为何能卖出200亿市值?

国无农不稳&#xff0c;民无粮不安。新时代的农村农民&#xff0c;需要现代化的农业作依托&#xff0c;而在农业现代化的过程中&#xff0c;品牌化、数字化成为至关重要的一环。 金秋十月&#xff0c;从南到北&#xff0c;从东到西&#xff0c;中国农村的每一块土地都洋溢着丰…