微信小程序获取当前位置 (在手机上点击没反应)

获取当前位置

写一个button,给个点击事件,先调用wx.getLocation,获取当前的经纬度,type类型为gcj02,在成功回调里调用wx.openLocation,参数填上经纬度,再在成功回调里调用wx.chooseLocation来选择当前位置(点击确定后)打印当前位置信息。

<button bindtap="getLocation">获取当前位置</button>
  getLocation() {
    wx.getLocation({
      type: 'gcj02', 
      success:function(res){
        console.log(res)
        wx.openLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          success:function(res){
            console.log(res)
          wx.chooseLocation({
            success:function(res){
              console.log(res)
            }
          })
          }
        })
      }
    })
  },

电脑上定位有偏差,建议手机调试

关于手机上点击按钮一直没反应

手机没开定位请打开手机定位 :)
不要问我怎么会有这个问题 :)

如果出现
在这里插入图片描述
在app.js里面添加
在这里插入图片描述

即可

发布了10 篇原创文章 · 获赞 16 · 访问量 890

猜你喜欢

转载自blog.csdn.net/MengJie_/article/details/105506604
今日推荐