Port Positioning Project Development Notes 2·WeChat Mini Program

Port Positioning Project Development Notes 2·WeChat Mini Program

Click the button to view your location on the map

Solve the problem of location authorization in the development of WeChat applet

Click the button to view your location on the map

demo.wxml

<view class="title">使用微信内置地图查看位置</view>
<button type="primary" bindtap="openLoca">查看当前位置</button>

demo.wxss

<view class="title">使用微信内置地图查看位置</view>
<button type="primary" bindtap="openLoca">查看当前位置</button>

demo.js

openLoca(){
    
    
    wx.getLocation({
    
    
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success (res) {
    
    
        const latitude = res.latitude
        const longitude = res.longitude
        wx.openLocation({
    
    
          latitude,
          longitude,
          scale: 18
        })
      }
     })
  },

Click to view the current location #### Solving the location authorization problem in the development of WeChat applets The previous article introduced how to obtain the current location code, but in many cases users will not open location authorization by themselves, so we need to prompt the user to open the location Authorization function.
wx.getLocation({
    
    
      success: res => {
    
    
        console.log(res);
      },
      fail: e => {
    
    
        console.log(e);
        // 判断用户是否拒绝了授权
        wx.getSetting({
    
    
          success: res => {
    
    
            if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
    
    
              // 用户拒绝了授权
              wx.showModal({
    
    
                title: '提示',
                content: '您拒绝了定位权限,将无法使用XX功能',
                success: res => {
    
    
                  if (res.confirm) {
    
    
                    // 跳转设置页面
                    wx.openSetting({
    
    
                      success: res => {
    
    
                        if (res.authSetting['scope.userLocation']) {
    
    
                          // 授权成功,重新定位
                          wx.getLocation({
    
    
                            success: res => {
    
    }
                          });
                        } else {
    
    
                          // 没有允许定位权限
                          wx.showToast({
    
    
                            title: '您拒绝了定位权限,将无法使用XX功能',
                            icon: 'none'
                          });
                        }
                      }
                    });
                  }
                }
              });
            }
          }
        });
    }
  })

Insert picture description here

Guess you like

Origin blog.csdn.net/zjlwdqca/article/details/112147349