微信小程式根據當前所在地址來獲取城市名稱
阿新 • • 發佈:2019-01-10
場景:
html部分:
<view class="index-top-left incursor" bindtap="get_user_address">
<span style="font-size:14px;color:#333333;">{{userCity}}</span>
</view>
js部分:獲取地址後傳給
data: { //------------------------------------------swiper design userCity: "北京市", }, get_user_address: function () { let that = this; wx.getLocation({ type: 'gcj02', //返回可以用於wx.openLocation的經緯度 success: function (res) { var latitude = res.latitude var longitude = res.longitude console.log(res.latitude, res.longitude) iFunctions._getCityName(res.latitude, res.longitude, that); } }) }, var iFunctions = { _getCityName: function (latitude, longitude, that) { wx.request({ url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=SB7BZ-6VKHO-LX4WZ-S2H4X-3DBG5-BCBLE', data: {}, success: function (res) { // console.log("逆地址解析", res); // console.log("逆地址解析", res.data.result.address_component.city); that.setData({ userCity: res.data.result.address_component.city }); }, fail: function (res) { } }) } };