获取地址、城市

uni.getSetting({

success(res) { 

if (!res.authSetting['scope.userLocation']) {

// 未授权

uni.authorize({

scope: 'scope.userLocation',

success() { //1.1 允许授权

  uni.getLocation({

type: 'gcj02',

geocode:true,//设置该参数为true可直接获取经纬度及城市信息

success: function (res) {

uni.setStorageSync("locationLat",res.latitude)

uni.setStorageSync("locationLng",res.longitude)

uni.request({

  url: 'http://api.map.baidu.com/reverse_geocoding/v3/?ak=Tolbv6WmfNq8bMNaT729T9sxuGRoysYH&location=' + res.latitude + ',' + res.longitude + '&output=json',

  data: {},

  header: {

    'Content-Type': 'application/json'

  },

  success: function (res) {

  if(res.data.status == 0){

  uni.setStorageSync("city_name",res.data.result.addressComponent.city)

  that.nvConfig.address.county = res.data.result.addressComponent.city


  uni.setStorageSync("index_province_name",res.data.result.addressComponent.province)

  uni.setStorageSync("index_city_name",res.data.result.addressComponent.city)

  that.getdata()

  that.loadData(1);

  }

  },

})

},

  });

},


})

}

that.getdata()

}

});

你可能感兴趣的:(获取地址、城市)