微信小程序---自动定位,天气预报


1.app.json页面

{
  "pages":[
    "pages/main/main"
  ],
  "window":{
    "backgroundTextStyle":"light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "天气APP",
    "navigationBarTextStyle":"black"
  }
}



2.app.wxss

/**app.wxss**/
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 

3.app.js

//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData:{
    userInfo:null
  }
})

4.首先通过经纬度查询本地城市,再通过城市名称查询出天气信息

 loadInfo:function(){
    var page=this;  
    wx.getLocation({
    type: 'gcj02', //返回可以用于wx.openLocation的经纬度
    success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log(latitude,longitude);
        page.loadCity(latitude,longitude);
    }
    })
  },
  loadCity:function(latitude,longitude){
    var page=this;  
     wx.request({
        url: 'http://api.map.baidu.com/geocoder/v2/?ak=D6WOzHaymzVVKvgiy8UbhQEznkgeK6BD&location='+latitude+','+longitude+'&output=json',
        header: {
            'Content-Type': 'application/json'
        },
        success: function(res) {
           console.log(res);
           var city = res.data.result.addressComponent.city;
           city=city.replace("市","");
           page.setData({city:city});
           page.loadWeather(city);
        }
        });
  },
  loadWeather:function(city){
    var page=this;
    wx.request({
        url: 'http://wthrcdn.etouch.cn/weather_mini?city='+city,
        header: {
            'Content-Type': 'application/json'
        },
        success: function(res) {
           
            console.log(res)
            var future=res.data.data.forecast;
            var todayInfo=future.shift();
            var today=res.data.data;
            today.todayInfo=todayInfo;
            page.setData({today:today,future:future})


        }
        });
  }


5.页面代码

<view class="content">
    <view class="today">
        <view class="info">
            <view class="temp">  {{today.wendu}}℃ </view>
            <view class="weather"> {{today.todayInfo.type}} {{today.todayInfo.fengxiang}} {{today.todayInfo.fengli}}</view>
            <view > 友情提示: {{today.ganmao}}</view>
            <view class="city"> {{city}}</view>
        </view>
    </view>

<import src="../template/itemtpl"/>
<view class="future" >
    <block wx:for="{{future}}">
         <template is="future-item" data="{{item}}"/>
    </block>
 </view>

</view>

样式:

.content{
    font-family : 微软雅黑,宋体;
    font-size: 14px;
    background-size:cover;  
    height: 100%;
    width:100%;
    background-image: url("../assets/img/bg.png");
    color:#FFFFFF;
}
.info{
    padding:20px;
    background:#FFFFFF;
    background:rgba(0, 0, 0, 0.5);
    box-shadow:10px 10px 20px rgba(0,0,0,0.5);
}
.today{
    padding-top: 50rpx;
    height: 50%;
     
}
.city{
    color:white;
    font-size: 16px;
    text-align: right;
    margin-right:10rpx;
    margin-top:30rpx;
}
.temp{
    font-size: 50px;
    text-align: center;
}
.weather{
    text-align: center;
}
.future{
     display: flex;
     flex-direction: row;
     height: 30%;
     padding-left: 5rpx;
     background:#FFFFFF;
     background:rgba(0, 0, 0, 0.1);
     box-shadow:10px 10px 20px rgba(0,0,0,0.5);
     text-align: center;
     padding-top:10rpx;
     padding-bottom:20rpx;
}
.future-item{
    min-height:100%;
    width: 165rpx;
    margin-left: 10rpx;
    margin-right: 10rpx;
    border: 1px solid coral;
    border-radius:10px;
    padding-top:10rpx;

}
.future-item view{
    margin-top:10px;
}

6.展示未来天气使用了模板

<template name="future-item">
    <view class="future-item" >
            <view>{{item.date}} </view>
            <view> {{item.type}} </view>
            <view>{{item.fengxiang}} {{item.fengli}}</view>
            <view> {{item.low}} </view>
            <view> {{item.high}} </view>
    </view>
</template>

需要源码可与我留言联系!!!大笑奋斗

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值