uni-app根据城市匹配对应的天气
类似如图功能;
1.引入cityInfo.json
const cityInfo = require('@/components/al-weather/city.json');
声明变量:
city:'', //本地城市名称
weather:'', //天气信息
2.获取城市名称——匹配城市的code值
uni.getLocation({
type: "gcj02",
altitude: false,
geocode: true,
success: function (res) {
var city = ret.address.city;
if(cityInfo.length > 0) {
cityInfo.map(item=>{
if(item['中文名']==this.city){
this.getWeather(item.adcode)
}else {
this.weather = '天气'
}
})
}
}
});
4.获取天气信息
根据自己的情况自行拼接weather
//获取天气信息
getWea