天天看點

微信小程式根據目前所在位址來擷取城市名稱

我的需求,頁面一打開就擷取目前的省份!!!

app.json 配置

"permission": {
    "scope.userLocation": {
      "desc": "你的位置資訊将用于定位目前省份"
    }
  }
           

index.js

onReady: function () {  
        let _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        _this.getProvinceName(latitude, longitude)
      }
    })
  },
getProvinceName(latitude, longitude){
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=填寫你自己的key',   
      data:{},
      success: (res)=> {
        console.log(res)
       // res.data.result.address_component.province   就是我要擷取的省份了;
        });
      }
    })
  },
           
微信小程式根據目前所在位址來擷取城市名稱

在開發的時候要注意一個點,就是騰訊位置的配置

打開騰訊位置網址https://lbs.qq.com/  登入賬号,接着按如下圖操作即可

微信小程式根據目前所在位址來擷取城市名稱