Applet positioning, Tencent access to location services

Access location services Tencent official document: https://lbs.qq.com/qqmap_wx_jssdk/index.html

A. Tencent location service access step

  1. Public registration number scan code, choose a small program you want to authorize to authorize success
  2. Then the page will become Tencent location services
    Here Insert Picture Description
  3. Register a new account
    Here Insert Picture Description
  4. registration success
    Here Insert Picture Description
  5. Application key
    Here Insert Picture Description
  6. Configure key
    Here Insert Picture Description
    fill WebServiceAPI authorized or licensed IP whitelist
  7. Set in the micro-letter domain name applet
    development -> Development Settings -> request legitimate domain name add https://apis.map.qq.com
    Here Insert Picture Description

Second, in the applet initialization Tencent location services

  1. Download the micro-channel applet JavaScriptSDK down, and put the new js lib folder
  2. Load js introduced
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
    onLoad: function () {
        // 实例化API核心类
        qqmapsdk = new QQMapWX({
            key: '申请的key'
        });
    }
})

Third, the positioning in the applet

Small micro-channel programs - get the current location of the city
1, acquires the current location, the user must first get authorization wx.openSetting;
2, the micro-channel interfaces getLocation acquires the current user location (micro-channel is returned latitude and longitude, speed, etc. parameters) ;
3, qqmapsdk.reverseGeocoder location services Tencent Tencent map inverse address resolution method to obtain the name of the current position of the coordinates;

Applet has a wx.getLocationmethod to obtain the latitude and longitude positioning can be achieved, but first need to allow the user to look at the permissions

// miniprogram/pages/index.js
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
  data: {
    cost: '--',
    scale: 18,
    longitude: '',
    latitude: '',
    markers: []
  },
  submit_order(){
    wx.navigateTo({
      url: '/pages/user/waitrec/waitrec'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        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
                      this.getAddress();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else {
          //调用wx.getLocation的API
          this.getAddress();
        }
      }
    });
    //判断全局变量寄件信息和收件信息,都存在则显示费用
    var app = getApp();
    if (app.globalData.send_info.address && app.globalData.receive_info.address) {
      this.setData({
        cost: 100
      })
    }
  },
  //获取定位信息
  getAddress(){
    // 实例化腾讯地图API核心类
    qqmapsdk = new QQMapWX({
      key: 'LXKBZ-IQY6X-XAM4F-ZTULL-OMKDQ-IBB3U'
    });
    var that = this;
    //获取当前位置
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var lat = res.latitude;
        var lon = res.longitude;
        //根据坐标获取当前位置名称,腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: { latitude: lat, longitude: lon },
          success: function (res) {
            var address = res.result.address;
            that.setData({
              latitude: lat,
              longitude: lon,
              markers: [{
                id: '1',
                iconPath: "../../../images/icon_cur_position.png",
                width: 22,
                height: 32,
                latitude: lat,
                longitude: lon,
                callout: {
                  content: address,
                  color: "#393939",
                  textAlign: 'center',
                  fontSize: 13,
                  borderRadius: 20,
                  bgColor: "#ffffff",
                  padding: 10,
                  display: "ALWAYS"
                },
              }]
            })
          }
        });
      }
    });
  }
})

Relevant official website
wx.getLocation
authorization

Guess you like

Origin blog.csdn.net/qq_34664239/article/details/92767493