WXML
<view class="map-weather"> <view><text>城市:</text>{{address}}</view> <view><text>天气:</text>{{weather}}</view> <view><text>温度:</text>{{temperature}}℃</view> <view><text>风力:</text>{{windpower}}级</view> <view><text>湿度:</text>{{humidity}}%</view> <view><text>风向:</text>{{winddirection}}</view> </view>
JS
const app = getApp(); const amap = app.data.amap; const key = app.data.key; Page({ data: { address:'', weather:'', temperature:'', humidity:'', windpower:'', winddirection:'' }, onLoad(){ var _this = this; var myAmap = new amap.AMapWX({ key: key }); myAmap.getWeather({ type: 'live', success(data) { if(data.city){ _this.setData({ address: data.liveData.city, humidity: data.liveData.humidity, temperature: data.liveData.temperature, weather: data.liveData.weather, winddirection: data.liveData.winddirection, windpower: data.liveData.windpower }) } }, fail() { wx.showToast({ title: '失败!' }) } }) } })
WXSS
page{ width: 100%; height: 100%; background-color: lightseagreen; color:#fff; } .map-weather{ position: fixed; top: 50%; left: 50%; transform: translateY(-50%) translateX(-50%); } .map-weather view{ height: 100rpx; line-height: 100rpx; font-size: 30rpx; }
另外,本站在线工具小程序上有一款天气查询工具,还添加了城市选择的功能,感兴趣的朋友可以扫描如下小程序码查看:
希望本文所述对大家微信小程序开发有所帮助。