手机显示服务器不工作,我想创建本地天气App它在本地服务器上完美运行但不能在线工作...

这是我的应用程序的JavaScript代码

var APPID = "fa89e6e41d40d7766082a8eb31cb25bb";

var temp;

var loc;

var icon;

var humidity;

var wind;

var direction;

function update(weather) {

icon.src = "imgs/codes/" + weather.code + ".png"

humidity.innerHTML = weather.humidity;

wind.innerHtml = weather.wind;

direction.innerHTML = weather.direction;

loc.innerHTML = weather.location;

temp.innerHTML = weather.temp;

}

window.onload = function () {

temp = document.getElementById("temperature");

loc = document.getElementById("location");

icon = document.getElementById("icon");

humidity = document.getElementById("humidity");

wind = document.getElementById("wind");

direction = document.getElementById("direction");

/* NEW */

if(navigator.geolocation){

var showPosition = function(position){

updateByGeo(position.coords.latitude, position.coords.longitude);

}

navigator.geolocation.getCurrentPosition(showPosition);

} else {

var zip = window.prompt("Could not discover your location. What is your zip code?");

updateByZip(zip);

}

}

/* NEW */

function updateByGeo(lat, lon){

var url = "http://api.openweathermap.org/data/2.5/weather?" +

"lat=" + lat +

"&lon=" + lon +

"&APPID=" + APPID;

sendRequest(url);

}

function updateByZip(zip){

var url = "http://api.openweathermap.org/data/2.5/weather?" +

"zip=" + zip +

"&APPID=" + APPID;

sendRequest(url);

}

function sendRequest(url){

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

var data = JSON.parse(xmlhttp.responseText);

var weather = {};

weather.code = data.weather[0].id;

weather.humidity = data.main.humidity;

weather.wind = data.wind.speed;

/* NEW */

weather.direction = degreesToDirection(data.wind.deg)

weather.location = data.name;

/* NEW */

weather.temp = K2F(data.main.temp);

update(weather);

}

};

xmlhttp.open("GET", url, true);

xmlhttp.send();

}

function degreesToDirection(degrees){

var range = 360/16;

var low = 360 - range/2;

var high = (low + range) % 360;

var angles = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];

for( i in angles ) {

if(degrees >= low && degrees < high){

console.log(angles[i]);

return angles[i];

console.log("derp");

}

low = (low + range) % 360;

high = (high + range) % 360;

}

return "N";

}

function K2F(k){

return Math.round(k*(9/5)-459.67);

}

function K2C(k){

return Math.round(k - 273.15);

}

Html代码

Weather App

Temprature: 0°

Location: Unknown

humidity: 0%

Wind: 0 mph N

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值