微信小程序獲取地理位置


小程序只支持獲取當前位置的經緯度,並不能直接獲取到地理名稱,需要通過第三方來逆地址解析,這里我選擇的是騰訊位置服務

在使用前需要去申請key,這里是地址:https://lbs.qq.com/console/mykey.html?console=mykey
下面上栗子:

<view class="hotcity-common thisCity">當前選擇城市</view>
  <view class="thisCityName">{{city}}</view>
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
page({
    data: {
    city: "",
    latitude: '',
    longitude: '',
    }
    
onLoad: function() {
    qqmapsdk = new QQMapWX({
      key: 'FD2BZ-R34KJ-4P4FW-KAW2S-ZASLV-GCFBR' //自己的key秘鑰 
    });
    this.getUserLocation();
  },
 
getUserLocation: function() {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log("設置信息"+JSON.stringify(res))
        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
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授權失敗',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //調用wx.getLocation的API
          vm.getLocation();
        } else {
          //調用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },
 
  // 微信獲得經緯度
  getLocation: function() {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function(res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  // 獲取當前地理位置
  getLocal: function(latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function(res) {
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude
        })
 
      }
    });
  },
 
})

以上就是小程序獲取地理位置的全部方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM