zoukankan      html  css  js  c++  java
  • 小程序 获取地理位置-- wx.getLocation

    话不多说直接上栗子

    //首先声明变量
    data:{ showLocationAuth:fasle }
    //这是第一种逻辑实现方式 点击按钮
    //当第一次点击授权按钮,用户取消授权之后,就会显示 授权当前定位按钮 点击之后就会触发系统授权系统授权成功之后在次点击 getShopLocationPoint 方法 就能拿到经纬度

    <button   wx:if="{{!showLocationAuth}}"   class="get_location_btn"   bindtap='getShopLocationPoint'   >   点我  </button>

    <button wx:if="{{showLocationAuth}}" class="get_location_btn" open-type="openSetting" > 授权当前定位 </button>
     
    /*************我是分割线************/
     
    //这是第二种逻辑 实现方式 
    onShow(){
      this.getShopLocationPoint(); 在这里调用是因为进入页面需要获取,如果用户没有授权,就去点击wxml的按钮唤醒系统授权,开启系统授权之后关闭系统页面,ohShow 之后就能拿到经纬度 
    }
    <button 
      wx:if="{{showLocationAuth}}" 
      class="get_location_btn"
      open-type="openSetting"
    >
      授权当前定位
    </button>
    //这是需要调用系统授权,只要授权之后 就会触发 onShow 里面的
    this.getShopLocationPoint() 就会获取到用户的经纬度信息
    
    

    公共代码

      getShopLocationPoint() {
        this.setData({
          showLocationAuth: false
        });
       wx.getLocation({
          type: "wgs84", // 默认wgs84
          success: res => {
            this.setData({
              showLocationAuth: false
            });
            console.log(res)
          },
          fail: res => {
            this.setData({
              showLocationAuth: true
            });
            console.log(res);
          }
        });
    
      },

    有错误的地方还望大神指点一二

  • 相关阅读:
    GitLab用户权限管理
    类似vant中的tab实现
    Gitgitee/github/gitlab账号分离
    Vim操作
    partition by 用法
    crontab执行feat_gen.sh时,报错找不到pyspark
    SQL同一个字段出现null和0值,有何区别,原因是什么?left join导致null值出现,case when导致0值出现
    linux 定时任务crontab的用法
    卡方检验
    ROC与AUC
  • 原文地址:https://www.cnblogs.com/BeautifulBoy/p/9790309.html
Copyright © 2011-2022 走看看