return $r; } /** * @param int $page 页数 * @param int $pagesize 每页显示数量 * @return mixed */ function link_find($page = 1, $pagesize = 100) { $arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize); return $arr; } /** * @param $id * @return bool 返回FALSE失败 TRUE成功 */ function link_delete($id) { if (empty($id)) return FALSE; $r = link__delete(array('id' => $id)); link_delete_cache(); return $r; } //--------------------------kv + cache-------------------------- /** * @return mixed 返回全部友情链接 */ function link_get($page = 1, $pagesize = 100) { $g_link = website_get('friends_link'); if (empty($g_link)) { $g_link = link_find($page, $pagesize); $g_link AND website_set('friends_link', $g_link); } return $g_link; } // delete kv and cache function link_delete_cache() { website_set('friends_link', ''); return TRUE; } ?>javascript - How to display OpenWeatherMap icons in weather application? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display OpenWeatherMap icons in weather application? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to display weather icons from OpenWeatherMap in my weather application using JavaScript.

I've tried using jQuery and other solutions I've seen online. I've also tried specifying this link ("/") using the "src" attribute, but the link doesn't work and the image is broken as a result.

How do I successfully add weather icons to my application?

Here is some minimal code used to formulate this problem. I hope this helps.

HTML:

<div class="weather">
  <div id="date"></div>
  <div id="cityName"></div>
  <img src="" id="icon">
  <div id="temp"></div>
  <div id="description"></div>
</div>

JavaScript:

var d = new Date();
var n = d.toLocaleDateString();
document.getElementById("date").innerHTML = n;

function getWeather( cityID ) {
  var key = '535f8a50b4bc24608c72fcde2aecb52b';
  fetch('.5/weather?id=' + cityID+ '&appid=' + key)  
  .then(function(resp) { return resp.json() }) 
  .then(function(data) {
    drawWeather(data);
  })
  .catch(function() {
    // catch any errors
  });
}

window.onload = function() {
  getWeather( 6167865 );
}

function drawWeather( d ) {
  var celcius = Math.round(parseFloat(d.main.temp)-273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32); 

  document.getElementById('cityName').innerHTML = d.name;
  document.getElementById('description').innerHTML = d.weather[0].description;
  document.getElementById('temp').innerHTML = fahrenheit + '&deg;';
  document.getElementById('icon').src = "/"+obj.weather[0].icon+".png";
 }

I'm trying to display weather icons from OpenWeatherMap in my weather application using JavaScript.

I've tried using jQuery and other solutions I've seen online. I've also tried specifying this link ("http://openweathermap/img/w/") using the "src" attribute, but the link doesn't work and the image is broken as a result.

How do I successfully add weather icons to my application?

Here is some minimal code used to formulate this problem. I hope this helps.

HTML:

<div class="weather">
  <div id="date"></div>
  <div id="cityName"></div>
  <img src="" id="icon">
  <div id="temp"></div>
  <div id="description"></div>
</div>

JavaScript:

var d = new Date();
var n = d.toLocaleDateString();
document.getElementById("date").innerHTML = n;

function getWeather( cityID ) {
  var key = '535f8a50b4bc24608c72fcde2aecb52b';
  fetch('https://api.openweathermap/data/2.5/weather?id=' + cityID+ '&appid=' + key)  
  .then(function(resp) { return resp.json() }) 
  .then(function(data) {
    drawWeather(data);
  })
  .catch(function() {
    // catch any errors
  });
}

window.onload = function() {
  getWeather( 6167865 );
}

function drawWeather( d ) {
  var celcius = Math.round(parseFloat(d.main.temp)-273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32); 

  document.getElementById('cityName').innerHTML = d.name;
  document.getElementById('description').innerHTML = d.weather[0].description;
  document.getElementById('temp').innerHTML = fahrenheit + '&deg;';
  document.getElementById('icon').src = "http://openweathermap/img/w/"+obj.weather[0].icon+".png";
 }
Share Improve this question asked May 19, 2021 at 18:23 adrvncadrvnc 271 silver badge7 bronze badges 1
  • Try using weatherdbi.herokuapp.. Though it is very new, it is very easy to use. – Knowledge Man Commented Apr 18, 2022 at 14:51
Add a ment  | 

4 Answers 4

Reset to default 4

Replace d.weather[0].icon with existing one when you set url in image src property.

var d = new Date();
var n = d.toLocaleDateString();
document.getElementById("date").innerHTML = n;

function getWeather( cityID ) {
  var key = '535f8a50b4bc24608c72fcde2aecb52b';
  fetch('https://api.openweathermap/data/2.5/weather?id=' + cityID+ '&appid=' + key)  
  .then(function(resp) { return resp.json() }) 
  .then(function(data) {
    drawWeather(data);
  })
  .catch(function() {
    // catch any errors
  });
}

window.onload = function() {
  getWeather( 6167865 );
}

function drawWeather( d ) {
  var celcius = Math.round(parseFloat(d.main.temp)-273.15);
  var fahrenheit = Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32); 

  document.getElementById('cityName').innerHTML = d.name;
  document.getElementById('description').innerHTML = d.weather[0].description;
  document.getElementById('temp').innerHTML = fahrenheit + '&deg;';
  document.getElementById('icon').src = `http://openweathermap/img/w/${d.weather[0].icon}.png`;
 }
<div class="weather">
  <div id="date"></div>
  <div id="cityName"></div>
  <img src="" id="icon">
  <div id="temp"></div>
  <div id="description"></div>
</div>

The open weather API documentation given the url to access icons. To display weather icon on webpage use the following method:

var iconCode = data.weather[0].icon; 
console.log(iconCode);
out_icon.innerHTML="<img 
src=http://openweathermap/img/wn/"+iconCode+"@2x.png>";

Here is the document for using weather icons: https://openweathermap/weather-conditions

In your case, you can change

document.getElementById('icon').src = "http://openweathermap/img/w/"+obj.weather[0].icon+".png";

to be

document.getElementById('icon').src = "http://openweathermap/img/wn/"+ data.weather[0].icon +"@2x.png";

replace

document.getElementById('icon').src="http://openweathermap/img/w/"+obj.weather[0].icon+".png";

with

document.getElementById('icon').src="http://openweathermap/img/w/"+d.weather[0].icon+".png";

发布评论

评论列表(0)

  1. 暂无评论