Weather_wev8.js 11.5 KB
if(typeof(Mobile_NS) == 'undefined'){
	Mobile_NS = {};
}

Mobile_NS.showCurrentCityWeather = function (theId,mecJson){
	Mobile_NS.getCurrentPosition(function(result){
	    var status = result["status"];
	    if(status == "1"){
	        var city = "Shanghai"; 
	        Mobile_NS.initWeather(theId,city);
	    }
	});
}

Mobile_NS.initWeather = function(theId,cityname,mecJson) {
    var GaoDeWeatherUrl = "https://restapi.amap.com/v3/weather/weatherInfo?key=fbbb3b85bc2133d171a6540deb92ca21&extensions=all&city=";
    var selectedCity = "shanghai";
	var placeholder = "";
	var unit = "c";
	if(typeof(cityname)=="string"&&cityname!=""){
		selectedCity = cityname;
	}else{
		if(mecJson){
			selectedCity = mecJson["city"];
		}else{
			var showCity = $("#weathercity_"+theId).val();
			if(typeof(showCity)=="string"&&showCity!=""){
				selectedCity = showCity; 
			}
		}
	}
	
	$("#weathercity_"+theId).val(selectedCity);
    getWoeid(selectedCity,theId);
    
    $("#weathercity_"+theId).keypress(function() {
        if (event.which == 13) {
            selectedCity = $("#weathercity_"+theId).val();
            getWoeid(selectedCity,theId);
            $("#weathercity_"+theId).blur();
        }
    });

    $("#weathercity_"+theId).focus(function(event) {
        placeholder = $("#weathercity_"+theId).val();
       $("#weathercity_"+theId).val("");
    });

    $("#weathercity_"+theId).blur(function(event) {
        if ($("#weathercity_"+theId).val() == "") {
            $("#weathercity_"+theId).val(placeholder);
        }
    });
    
    function getWoeid(weathercity,theId) {
    	var woeid =  WeatherCityCode[weathercity.toLowerCase()];
    	if(woeid&&!isNaN(woeid)){
    		getWeatherInfo(woeid,theId);
    	}else{
		    var jsonURL = GaoDeWeatherUrl + weathercity;
		    showLoading(theId);
		    $.getJSON(jsonURL, function(data){
		    	woeidDownloaded(data,theId);
		    });
    	}
	}
    
    function showLoading(theId){
    	var weatherContainer = $("#weatherContainer_"+theId);
    	var city = $("#weathercity_"+theId);
    	var cityspan = $("#weathercity_"+theId+"_span");
    	var topDiv = city.parent();
    	var h = topDiv.height();
    	if(cityspan.length==0){
    		topDiv.append("<div style='color: white;padding-top:15px;va;font-size: 14px;display:none;' id='weathercity_"+theId+"_span' >正在加载天气...</div>");
    	}
    	var bot = $(".bot",weatherContainer);
    	var top = $(".top",weatherContainer);
    	bot.css("height",(bot.height()-20)+"px");
    	$("#weathercity_"+theId+"_span").css("height",(top.height()-30-15)+"px");;
    	$("#weathercity_"+theId+"_span").show();
    	$(".bot li img",weatherContainer).hide();
    	$(".bot li p",weatherContainer).hide();
    	$("#deg_"+theId).hide();
    	city.hide();
    	cityspan.show();
    }
    
    function hideLoading(theId){
    	var weatherContainer = $("#weatherContainer_"+theId);
    	var city = $("#weathercity_"+theId);
    	var cityspan = $("#weathercity_"+theId+"_span");
    	
    	var bot = $(".bot",weatherContainer);
    	bot.css("height","");
    	$(".bot li img",weatherContainer).show();
    	$(".bot li p",weatherContainer).show();
    	
    	$("#deg_"+theId).show();
    	city.show();
    	cityspan.hide();
    }
	
	function woeidDownloaded(data,theId) {
	    var woeid = null;
        if (data.count <= 0) {
	        $("#weathercity_"+theId).val("No city found");
	        $("#deg_"+theId).html("");
            setImage("未知", $("#big_"+theId)[0]);
            for (var i = 0; i <= 2; i++) {
	            $('#forecast' + i+"_"+theId).html("");
                setImage("未知", $("#forecastimg" + i+"_"+theId)[0]);
	            $('#forecastdeg' + i+"_"+theId).html("");
	        }
	        return;
        } else if (data.count == 1) {
            woeid = data.forecasts[0].adcode;
	    } else {
            woeid = data.forecasts[0].adcode;
	    }
	    getWeatherInfo(woeid,theId);
	}
	
	function getWeatherInfo(woeid,theId) {
        var jsonURL = GaoDeWeatherUrl + woeid;
	    $.getJSON(jsonURL,function(data){
	    	weaterInfoDownloaded(data,theId);
	    });
	}
	
	function weaterInfoDownloaded(data,theId) {
        $("#weathercity_"+theId).val(data.forecasts[0].city);
        $("#deg_"+theId).html((parseInt(data.forecasts[0].casts[0].daytemp) + parseInt(data.forecasts[0].casts[0].nighttemp)) / 2 + "°" + unit.toUpperCase());
        setImage(data.forecasts[0].casts[0].dayweather, $("#big_"+theId)[0]);
        for (var i = 0; i <= 2; i++) {
            var fc = data.forecasts[0].casts[i+1];
            $('#forecast' + i+"_"+theId).html(fc.date);
            setImage(fc.dayweather, $("#forecastimg" + i+"_"+theId)[0]);
            $('#forecastdeg' + i+"_"+theId).html((parseInt(fc.daytemp) + parseInt(fc.nighttemp)) / 2 + " °" + unit.toUpperCase());
	    }
	    $("#weatherContainer_"+theId).find(".bot img").css("visibility","visible");
	    hideLoading(theId);
	}
	
	function setImage(code, image) {
	    image.src = "/mobilemode/images/weathericon/";
	    switch (code) {
            case "晴":
                image.src += "Sun.png"
                break;
            case "少云":
                image.src += "Cloud.png"
                break;
            case "晴间多云":
                image.src += "Cloud-Sun.png"
                break;
            case "多云":
                image.src += "Cloud.png"
                break;
            case "阴":
                image.src += "Cloud.png"
                break;
            case "有风":
                image.src += "Wind.png"
                break;
            case "平静":
                image.src += "Wind.png"
                break;
            case "微风":
                image.src += "Wind.png"
                break;
            case "和风":
                image.src += "Wind.png"
                break;
            case "清风":
                image.src += "Wind.png"
                break;
            case "强风劲风":
                image.src += "Wind.png"
                break;
            case "疾风":
                image.src += "Wind.png"
                break;
            case "大风":
                image.src += "Wind.png"
                break;
            case "烈风":
                image.src += "Wind.png"
                break;
            case "风暴":
                image.src += "Cloud-Lightning.png"
                break;
            case "狂爆风":
                image.src += "Wind.png"
                break;
            case "飓风":
                image.src += "Wind.png"
                break;
            case "热带风暴":
                image.src += "Cloud-Lightning.png"
                break;
            case "阵雨":
                image.src += "Cloud-Drizzle-Alt.png"
                break;
            case "雷阵雨":
                image.src += "Cloud-Lightning.png"
                break;
            case "雷阵雨并伴有冰雹":
                image.src += "Cloud-Lightning.png"
                break;
            case "小雨":
                image.src += "Cloud-Drizzle-Alt.png"
                break;
            case "中雨":
                image.src += "Cloud-Drizzle.png"
                break;
            case "大雨":
                image.src += "Cloud-Rain.png"
                break;
            case "暴雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "大暴雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "特大暴雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "强阵雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "强雷阵雨":
                image.src += "Cloud-Lightning.png"
                break;
            case "极端降雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "毛毛雨/细雨":
                image.src += "Cloud-Drizzle-Alt.png"
                break;
            case "雨":
                image.src += "Cloud-Drizzle.png"
                break;
            case "小雨-中雨":
                image.src += "Cloud-Drizzle.png"
                break;
            case "中雨-大雨":
                image.src += "Cloud-Rain.png"
                break;
            case "大雨-暴雨":
                image.src += "Cloud-Rain.png"
                break;
            case "暴雨-大暴雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "大暴雨-特大暴雨":
                image.src += "Cloud-Hail-Alt.png"
                break;
            case "雨雪天气":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "雨夹雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "阵雨夹雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "冻雨":
                image.src += "Cloud-Drizzle.png"
                break;
            case "雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "阵雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "小雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "中雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "大雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "暴雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "小雪-中雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "中雪-大雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "大雪-暴雪":
                image.src += "Cloud-Snow-Alt.png"
                break;
            case "浮尘":
                image.src += "Cloud-Fog.png"
                break;
            case "扬沙":
                image.src += "Cloud-Fog.png"
                break;
            case "沙尘暴":
                image.src += "Cloud-Fog.png"
                break;
            case "强沙尘暴":
                image.src += "Cloud-Fog.png"
                break;
            case "龙卷风":
                image.src += "Tornado.png"
                break;
            case "雾":
                image.src += "Cloud-Fog.png"
                break;
            case "浓雾":
                image.src += "Cloud-Fog.png"
                break;
            case "强浓雾":
                image.src += "Cloud-Fog.png"
                break;
            case "轻雾":
                image.src += "Cloud-Fog.png"
                break;
            case "大雾":
                image.src += "Cloud-Fog.png"
                break;
            case "特强浓雾":
                image.src += "Cloud-Fog.png"
                break;
            case "霾":
                image.src += "Cloud-Fog.png"
                break;
            case "中度霾":
                image.src += "Cloud-Fog.png"
                break;
            case "重度霾":
                image.src += "Cloud-Fog.png"
                break;
            case "严重霾":
                image.src += "Cloud-Fog.png"
                break;
            case "热":
                image.src += "Sun.png"
                break;
            case "冷":
                image.src += "Thermometer-Zero.png"
                break;
            case "未知":
                image.src += "Moon-New.png"
                break;
            default:
	            image.src += "Moon-New.png"
	            break;
	    }
	}
}