JS源生調用天氣事件風向


jsonp('https://api.map.baidu.com/api?v=2.0&ak=Dv1NMU23dh1sGS9n2tUouDEYY96Dfzh3&s=1&callback=getCity');
window.onload
=function(){
//請求天氣車數據
btn.onclick=function (){
jsonp(createUrl()[
0]);
jsonp(createUrl()[
1]);
}
};

function getCity(){
function city(result){
//去掉城市名后的"市"
var city = result.name.substring(0,result.name.length-1);
//請求當前城市的天氣數據
jsonp(createUrl(city)[0]);
jsonp(createUrl(city)[
1]);
}
var cityName = new BMap.LocalCity();
cityName.get(city);
}

// 數據請求函數
function jsonp(url){
var script = document.createElement('script');
script.src
= url;
document.body.insertBefore(script, document.body.firstChild);
document.body.removeChild(script);
}

//數據請求成功回調函數,用於將獲取到的數據放入頁面相應位置
function getWeather(response) {
var oSpan = document.getElementsByClassName('info');
var data = response.result;
oSpan[
0].innerHTML=data[0].citynm;
oSpan[
1].innerHTML=data[0].days;
oSpan[
2].innerHTML=data[0].week;
oSpan[
3].innerHTML=data[0].weather;
oSpan[
4].innerHTML=data[0].temperature;
oSpan[
5].innerHTML=data[0].winp;
oSpan[
6].innerHTML=data[0].wind;

var aDiv = document.getElementsByClassName('future_box');
for(var i=0; i<aDiv.length; i++){
var aSpan = aDiv[i].getElementsByClassName('future_info');
aSpan[
0].innerHTML = data[i+1].days;
aSpan[
1].innerHTML = data[i+1].week;
aSpan[
2].innerHTML =data[i+1].weather;
aSpan[
3].innerHTML = data[i+1].temperature;
}
//根據返回數據,替換不同天氣圖片
changeImg(response);
}

function getTodayWeather(response){
var oSpan = document.getElementsByClassName('info');
var data = response.results;
oSpan[
7].innerHTML=data[0].pm25;
oSpan[
8].innerHTML=data[0].index[4].zs;
oSpan[
9].innerHTML=data[0].index[1].zs;
oSpan[
10].innerHTML=data[0].index[2].zs;
oSpan[
11].innerHTML=data[0].index[0].zs;
}

//根據獲取到的數據更改頁面中相應的圖片
function changeImg(data){
var firstImg = document.getElementsByTagName("img")[0];
var firstWeatherId=data.result[0].weatid;
chooseImg(firstWeatherId,firstImg);

var aImg = document.getElementById('future_container').getElementsByTagName('img');
for(var j=0; j<aImg.length; j++){
var weatherId = data.result[j+1].weatid;
chooseImg(weatherId,aImg[j]);
}
}

//選擇圖片
function chooseImg(id,index){
switch(id){
case '1':
index.src
='images/weather_icon/1.png';
break;
case '2':
index.src
='images/weather_icon/2.png';
break;
case '3':
index.src
='images/weather_icon/3.png';
break;
case '4':
case '5':
case '6':
case '8':
case '9':
case '10':
case '11':
case '12':
case '13':
case '20':
case '22':
case '23':
case '24':
case '25':
case '26':
index.src
='images/weather_icon/4.png';
break;
case '7':
index.src
='images/weather_icon/6.png';
break;
case '14':
case '15':
case '16':
case '17':
case '18':
case '27':
case '28':
case '29':
index.src
='images/weather_icon/5.png';
break;
case '19':
case '21':
case '30':
case '31':
case '32':
case '33':
index.src
='images/weather_icon/7.png';
break;
default:
index.src
='images/weather_icon/8.png';
}
}

//根據城市名創建請求數據及url
function createUrl(){
var cityName = '';
if(arguments.length == 0) {
cityName
= document.getElementById('text').value;
}
else{
cityName
= arguments[0];
}
// console.log(cityName);
var urls = [];
urls[
0] = 'https://sapi.k780.com/?app=weather.future&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=getWeather&weaid=' + encodeURI(cityName);
urls[
1] = 'https://api.map.baidu.com/telematics/v3/weather?output=json&ak=FK9mkfdQsloEngodbFl4FeY3&callback=getTodayWeather&location=' + encodeURI(cityName);
return urls;
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com