Get current location by WeChat applet (2)

Now when you click on the following icon to get the current location information: 

 

wxml

  <view class="task-item">
    <view class="task-item-title">详细地址</view>
    <view class="task-item-input2">
      <input name='address' placeholder="请输入详细地址" value="{
   
   {address}}" type="text"  bindinput="getAddress" placeholder-class='rightinput'></input>
      <view class="place"  bindtap='choosePlace'>
        <image src="/images/icon/place.png" ></image>
      </view>
    </view>
  </view>

 js

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 //授权
    var that=this;
    wx.getLocation({
      type:"wgs84",
      success:res=>{

        that.setData({
          latitude:res.latitude,
          longitude:res.longitude
        })
      }
    }) 
  },   

 choosePlace(e) {
      let that = this
      console.log(e);
      var obj = e.target.dataset;
      wx.getSetting({
        success(res) {
          console.log(res)
          if (!res.authSetting['scope.userLocation']) {
            wx.openSetting({
              success(res) {
                console.log(res.authSetting)
              }
            })
          } else {
            wx.chooseLocation({
              success: ces => {
                console.log(ces)
                that.setData({
                  address: ces.address
                })
              }
            })
          }
        }
      });
    },

Click to show the current position: 

The positioning accuracy of the real phone on the mobile phone will be higher.

Guess you like

Origin blog.csdn.net/asteriaV/article/details/109649735