小程序——根据用户地址来获取城市名称

场景:

 html部分:       

 <view class="index-top-left incursor" bindtap="get_user_address">
                <span class="address"></span>
                <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) {

            }
        })
    }

猜你喜欢

转载自blog.csdn.net/weixin_38245489/article/details/81703842