tp5下的天气预报

 最近做一个项目,客户需要显示手机所在地的天气信息!

这里通过腾讯地图接口获取当前手机所在的位置信息,传递给后台根据位置信息获得相应的天气信息!

前端js获取位置信息:

<iframe id="geoPage" width=0 height=0 frameborder=0  style="display:none;" scrolling="no"
 src="https://apis.map.qq.com/tools/geolocation?key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp">
</iframe>
<script type="text/javascript">
  window.addEventListener('message', function(event) {
	  // 接收位置信息
	  var loc = event.data;
	  if(loc != null){
		 console.log('location', loc);
		 //alert(loc.city);
		 $.post("__HTTP__/index/index/weather",
		   {
			 city:loc.city
		   },
		   function(data,status){
			 console.log(data);
			 //alert("Data: "  + "\nStatus: " + status);
		   });
	  }
  }, true)
</script>

 后台获取城市名,查询相关天气信息:

(此处接口获取今后四天的天气信息,包括对应天气小图片)

*通过isNight()确定昼夜天气图片

    /**
        $city 城市名(如:北京)
        $temp 实时温度
        $weatherlist 今后四天天气
        $todayWeather 当天天气
        $bgImg 天气状况图标
    */
    public function weather($value='')
    {
        $city = input('post.city');
        $url = 'http://api.map.baidu.com/telematics/v3/weather?location='.$city.'&output=json&ak=EGgzZ22dsboWQEcPQ6KDQLknQd3YkkkP';
        $weather = json_decode(file_get_contents($url));
        if($weather->status){
            $weatherlist = $weather->results[0]->weather_data;
            $todayWeather = $weatherlist[0]->weather;
            preg_match_all('|\((.*)\)|U', $weatherlist[0]->date,$res);
            preg_match_all('/:(\S+)/', $res[1][0],$temp);
            $temp = $temp[1][0]; 
            //用于区分夜昼的天气图标
            $bgImg = $this->isNight()?$weatherlist[0]->nightPictureUrl:$weatherlist[0]->dayPictureUrl;
            $result = ['city'=>$city,'temp'=>$temp,'todayWeather'=>$todayWeather,'bgImg'=>$bgImg];
            //dump($result);
            return $result;
        }
    }

    /**
        默认白天:6:40 -- 18:40
    */
    public function isNight($bTime='6:40',$eTime='18:40')
    {
        $result = false;
        $bTime = strtotime(date('Ymd'.$bTime));
        $eTime = strtotime(date('Ymd'.$eTime));
        if(time()<$bTime||time()>$eTime)
            $result = true;
        return $result;
    }

$url = 'http://api.map.baidu.com/telematics/v3/weather?location='.$city.'&output=json&ak=EGgzZ22dsboWQEcPQ6KDQLknQd3YkkkP';此处ak是在http://lbsyun.baidu.com/apiconsole/key/create创建应用生成的!如果不想申请百度应用,可以直接使用下面 附录二 “其他天气接口”。

附:

根据经纬度获取城市信息:

    //根据经纬度获取位置信息
    public function place($lat='34.53952',$lng='113.39044')
    {
        $url = 'http://api.map.baidu.com/geocoder?location='.$lat.','.$lng.'&output=xml&key=28bcdd84fae25699606ffad27f8da77b';
        $site = simplexml_load_file($url);
        dump($site);
        $place = $site->result->addressComponent->district;
        echo $place."<br>";
        echo $site->result->formatted_address;
    }

其他天气接口:

    /**
        $city 城市名(如:北京)
        $weather 所有天气信息
        $temp 实时温度
        $bgImg 天气图片
        $forecast 今后五天预报
        $todayWeather 当天天气
    */
    public function weather($value='')
    {
        $city = input('post.city');
        $url = 'http://wthrcdn.etouch.cn/weather_mini?city='.$city;
        $html = file_get_contents($url);
        $weather = json_decode(gzdecode($html));
        $temp = $weather->data->wendu;
        $forecast = $weather->data->forecast;
        $todayWeather = $forecast['0']->type;
        $day = $this->isNight()?'night':'day';
        $img = $this->weatherImg($todayWeather);
        $bgImg = 'http://api.map.baidu.com/images/weather/'.$day.'/'.$img.'.png';
        return ['city'=>$city,'temp'=>$temp,'todayWeather'=>$todayWeather,'bgImg'=>$bgImg];
    }

    /**
        默认白天:6:40 -- 18:40
    */
    public function isNight($bTime='6:40',$eTime='18:40')
    {
        $result = false;
        $bTime = strtotime(date('Ymd'.$bTime));
        $eTime = strtotime(date('Ymd'.$eTime));
        if(time()<$bTime||time()>$eTime)
            $result = true;
        return $result;
    }

    //天气图片
    public function weatherImg($value='')
    {
        switch ($value) {
            case '晴':
                $img = 'qing';
                break;
            case '阴':
                $img = 'yin';
                break;
            case '多云':
                $img = 'duoyun';
                break;
            case '阵雨':
                $img = 'zhenyu';
                break;
            case '雷阵雨':
                $img = 'leizhenyu';
                break;
             case '雷阵雨伴有冰雹':
                $img = 'leizhenyubanyoubingbao';
                break;
             case '雨夹雪':
                $img = 'yujiaxue';
                break;
             case '小雨':
                $img = 'xiaoyu';
                break;
             case '中雨':
                $img = 'zhongyu';
                break;
             case '大雨':
                $img = 'dayu';
                break;
             case '暴雨':
                $img = 'baoyu';
                break;
             case '大暴雨':
                $img = 'dabaoyu';
                break;
             case '特大暴雨':
                $img = 'tedabaoyu';
                break;
             case '阵雪':
                $img = 'zhenxue';
                break;
             case '小雪':
                $img = 'xiaoxue';
                break;
             case '中雪':
                $img = 'zhongxue';
                break;
             case '大雪':
                $img = 'daxue';
                break;
             case '暴雪':
                $img = 'baoxue';
                break;
             case '雾':
                $img = 'wu';
                break;
             case '冻雨':
                $img = 'dongyu';
                break;
             case '沙尘暴':
                $img = 'shachenbao';
                break;
             case '浮尘':
                $img = 'fuchen';
                break;
             case '扬沙':
                $img = 'yangsha';
                break;
             case '强沙尘暴':
                $img = 'qiangshachenbao';
                break;
             case '霾':
                $img = 'mai';
                break;
            default:
                $img = '';
                break;
        }
        return $img;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值