天气 API (openweathermap) 显示温度错误
Weather API (openweathermap) Showing Temperature Wrong
这段代码中的所有内容我都在尝试为用户提供当地的位置和温度但是不知何故温度以摄氏度显示的方式更少而且下面也没有更新是我尝试的就像它是 4-5 小时后的数据少了 10 摄氏度,如果温度是 22(摄氏度)小时,它显示为 3(摄氏度)工作示例 Codepen http://codepen.io/cannelflow/full/RrymYo/
var x = document.getElementById("demo");
var y = document.getElementById("demo1");
window.onload = getLocation();
//window.onload=getWeather();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
//Location For Display
function showPosition(position) {
var loc = { lat: position.coords.latitude, lon: position.coords.longitude };
getWeather(loc);
var baseURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=";
var fullURL = baseURL + loc.lat + "," + loc.lon;
$.ajax({
url: fullURL,
success: function (display) {
x.innerHTML = display.results[1].formatted_address;
}
});
}
//Location For Weather
function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?lat=";
var appid = "064129b86c99c35c42d531db251b99e3";
var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
//http://api.openweathermap.org/data/2.5/weather?lat=21.2600668&lon=81.5989561&appid=064129b86c99c35c42d531db251b99e3&units=metric
$.ajax({
url: fullURL,
success: function (display1) {
y.innerHTML = display1.main.temp;
}
});
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
<body>
<section>
<div class="container-fluid text-center">
<br />
<!-- <h1><button class="btn btn-danger" onclick="getLocation()">Click Me To Get Your Location!</button></h1> -->
<h1 class="text-primary" id="demo1"></h1>
<br />
<h1 class="text-primary" id="demo"></h1>
</div>
</section>
</body>
你打错了:
var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
应该是
var fullURL = baseURL + loc.lat + "&lon=" + loc.lon + "&appid=" + appid + "&units=metric";
您的查询字符串有错字。更好的替代方法是用户 jQuery.param
从对象构建查询字符串,因为它更易于阅读,因此不易出错。
function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?";
return $.ajax({
url: baseURL + $.param({
appid: "064129b86c99c35c42d531db251b99e3",
lon: loc.lon,
lat: loc.lat,
units: "metric"
}),
success: function(display1) {
y.innerHTML = display1.main.temp;
}
});
}
像
一样修改它
function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?lat=";
var appid = "064129b86c99c35c42d531db251b99e3";
//var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
//http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&appid=44db6a862fba0b067b1930da0d769e98&units=metric
$.ajax({
url: baseURL,
type: 'get',
dataType: 'JSONP',
data: { lat: loc.lat, lon: loc.lon, units: 'metric', APPID: appid },
success: function (data) {
y.innerHTML = data['main']['temp'] + " °C";
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
成功了
这段代码中的所有内容我都在尝试为用户提供当地的位置和温度但是不知何故温度以摄氏度显示的方式更少而且下面也没有更新是我尝试的就像它是 4-5 小时后的数据少了 10 摄氏度,如果温度是 22(摄氏度)小时,它显示为 3(摄氏度)工作示例 Codepen http://codepen.io/cannelflow/full/RrymYo/
var x = document.getElementById("demo");
var y = document.getElementById("demo1");
window.onload = getLocation();
//window.onload=getWeather();
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
//Location For Display
function showPosition(position) {
var loc = { lat: position.coords.latitude, lon: position.coords.longitude };
getWeather(loc);
var baseURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=";
var fullURL = baseURL + loc.lat + "," + loc.lon;
$.ajax({
url: fullURL,
success: function (display) {
x.innerHTML = display.results[1].formatted_address;
}
});
}
//Location For Weather
function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?lat=";
var appid = "064129b86c99c35c42d531db251b99e3";
var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
//http://api.openweathermap.org/data/2.5/weather?lat=21.2600668&lon=81.5989561&appid=064129b86c99c35c42d531db251b99e3&units=metric
$.ajax({
url: fullURL,
success: function (display1) {
y.innerHTML = display1.main.temp;
}
});
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
<body>
<section>
<div class="container-fluid text-center">
<br />
<!-- <h1><button class="btn btn-danger" onclick="getLocation()">Click Me To Get Your Location!</button></h1> -->
<h1 class="text-primary" id="demo1"></h1>
<br />
<h1 class="text-primary" id="demo"></h1>
</div>
</section>
</body>
你打错了:
var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
应该是
var fullURL = baseURL + loc.lat + "&lon=" + loc.lon + "&appid=" + appid + "&units=metric";
您的查询字符串有错字。更好的替代方法是用户 jQuery.param
从对象构建查询字符串,因为它更易于阅读,因此不易出错。
function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?";
return $.ajax({
url: baseURL + $.param({
appid: "064129b86c99c35c42d531db251b99e3",
lon: loc.lon,
lat: loc.lat,
units: "metric"
}),
success: function(display1) {
y.innerHTML = display1.main.temp;
}
});
}
像
一样修改它function getWeather(loc) {
var baseURL = "http://api.openweathermap.org/data/2.5/weather?lat=";
var appid = "064129b86c99c35c42d531db251b99e3";
//var fullURL = baseURL + loc.lat + "&lon=" + loc.lat + "&appid=" + appid + "&units=metric";
//http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&appid=44db6a862fba0b067b1930da0d769e98&units=metric
$.ajax({
url: baseURL,
type: 'get',
dataType: 'JSONP',
data: { lat: loc.lat, lon: loc.lon, units: 'metric', APPID: appid },
success: function (data) {
y.innerHTML = data['main']['temp'] + " °C";
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
成功了