注册 登录 充值会员 退出
毕业设计 PHP源码
充值

jQuery全国城市天气预报API查询代码

作者/代码整理:  (转载请附加本文地址,带有“懒人原生”字样的谢绝转载) 发布日期:2018-06-13
jQuery全国城市天气预报API查询代码
一款简单好调用的jQuery全国城市天气预报API查询代码,输入城市名进行查询。


js代码

<script src="http://webapi.amap.com/subway?v=1.0&key=a6414d65323db9e2718b31e525a1337d&callback=cbk"></script>
<script>
	function getWeather(location,type,el){
		var url = "http://restapi.amap.com/v3/weather/weatherInfo";
		var postData = {
			key: "dfb9a576fbcb2c9a13a65ab736e47004",
			city: location,
			extensions: "all"
		};
		$.ajax({
			url:url,
			type:type,
			data:postData,
			success:function(status,data){
				console.log(status);
				var html1 = "";
				var html2 = "";
				var htmlOne = "";
				if(status.forecasts.length == 1){
					$(".data1").css("display","none");
					$(".data2").css("display","none");
					$(".data3").css("display","block");
					
					var weatherData = status.forecasts[0].casts;
					console.log(status.forecasts[0].province+"省"+status.forecasts[0].city);
					$(".cityName").html(status.forecasts[0].province+"省"+status.forecasts[0].city);
					$(".left_data").html(status.forecasts[0].reporttime	);
					$(".left_weather").html(weatherData[0].dayweather+" \ "+weatherData[0].nightweather);
					$(".left_temp").html(weatherData[0].daytemp+" \ "+weatherData[0].nighttemp);
					$(".left_wind1").html(weatherData[0].daywind+" \ "+weatherData[0].nightwind);
					$(".left_wind2").html(weatherData[0].daypower+" \ "+weatherData[0].nightpower);
					
					
					for(var i=1;i<weatherData.length;i++){
						htmlOne += '<li>'+weatherData[i].date+'</li><li>星期'+weatherData[i].week+'</li><li>'+weatherData[i].dayweather+'"\"'+weatherData[i].nightweather+'</li><li>'+weatherData[i].daytemp+'"\"'+weatherData[i].nighttemp+'</li><li>'+weatherData[i].daywind+'"\"'+weatherData[i].nightwind+'</li><li>'+weatherData[i].daypower+'"\"'+weatherData[i].nightpower+'</li>'
					}
					$(".dataOne").html(htmlOne);
					
					
				}else{
					$(".data1").css("display","block");
					$(".data2").css("display","block");
					$(".data3").css("display","none");
				}
				
				
			},
			error:function(status){
			}
		})
	}
	$(".seachBtn").click(function(){
		getWeather($(".intCity").val(),"post",".box1");
	})
	function addHtmlTwo(){
		
	}
	function addHtmlOne(){
		
	}
	
	
</script>