如何将天气图标从JSON更改为天气图标 我正在尝试将我从API获得的天气图标更改为我在Photoshop上绘制的天气图标!如果您需要翻译来理解,我的变量也是法语和评论。

问题描述 投票:0回答:2

创建一个对象,将每个天气图标的URL放入对象中,名称代表天气条件,然后使用正确的名称获取正确的URL:

var weatherIcons = {
   snowy:"/snowyicon.jpg",
   rainy:"/rainyicon.jpg",
   sunny:"/sunnyicon.jpg"
};

...

var condition = parsed_json['current_observation']['weather'];
var iconTOUse = weatherIcons[condition];

var element = '<img src="'+iconTOUse+'" />';
javascript html json weather-api
2个回答
1
投票

在这里:

 "<div class='icone'><img src='http://icons-ak.wxug.com/i/c/k/"+wicon+".gif' alt='"+condition+"' title='"+condition+"'/></div>"

0
投票

"<div class='icone'><img src='http://YourServer.com/yourIcons/"+ wicon +".gif' alt='"+condition+"' title='"+condition+"'/></div>"

确保您的图标与他们相同。

现在,如果您坚持使用不同的名称,则需要将其名称映射到您的名字中,这应该很琐碎。

const weather = response.weather;

  if (weather === "Clear") {
    weatherIcon.src = "sun.png";
  } else if (weather === "Clouds") {
    weatherIcon.src = "cloud.png";
  } else if (weather === "Rain") {
    weatherIcon.src = "rain.png";
  } else {
    weatherIcon.src = "default.png";
  }

  console.log(response);
})
.catch(err => console.error(err));

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.