简体   繁体   English

如何显示openweather地图天气图标react.js

[英]how to display openweather map weather icons react.js

im trying to display weather icons from open weather map api , but am not entirely sure how to do it , here is the documentation https://openweathermap.org/weather-conditions .. im passing in weather.icon just like its written in the docs but its not working for some reason ,can someone please tell me what i am doing wrong? 即时通讯试图显示来自开放式天气地图api的天气图标,但并不完全确定该怎么做,这是文档https://openweathermap.org/weather-conditions ..即时通讯就像它的书面形式一样传入了weather.icon该文档,但由于某种原因而无法正常工作,有人可以告诉我我在做什么错吗? thanks 谢谢

app.js app.js

    class App extends React.Component {

  state = {
    temperature: undefined,
    city: undefined,
    country: undefined,
    pressure: undefined,
    humidity: undefined,
    description: undefined,
    rain:undefined,
    icon:undefined,
    error: undefined
  }

  handlenum1Change (evt) {



let temp = (evt.target.value);




}


 getWeather = async (e) => {
    e.preventDefault();
    const city = e.target.city.value;

    const api_call = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`);
    const data = await api_call.json();
    console.log(data)
    if (city) {
      this.setState({
        temperature: data.main.temp,
        city: data.name,

        icon: data.weather.icon,
        rain: data.rain,
        pressure: data.main.pressure,
        humidity: data.main.humidity,
        description: data.weather[0].description,
        error: ""
      });
    } else {
      this.setState({
        temperature: undefined,
        city: undefined,
        country: undefined,
        humidity: undefined,
        description: undefined,
        pressure:undefined,
        rain : undefined,
        error: "Please enter the values."
      });
    }
  }
  render() {
    return (
      <div>
        <div className="wrapper">
          <div className="main">
            <div className="container">
              <div className="row">
                <div className="col-xs-5 title-container">

                </div>
                <div className="col-xs-7 form-container">
                  <form onSubmit={this.getWeather} >

                  <input type="text" name="city" onChange={this.handlenum1Change} placeholder="City..."/>

    <button>Get Weather</button>
    </form>


                  <Weather 
                    temperature={this.state.temperature} 
                    humidity={this.state.humidity}
                    city={this.state.city}
                       pressure={this.state.pressure}
                    description={this.state.description}
                    rain={this.state.rain}
                    icon={this.state.icon}
                    error={this.state.error}
                  />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }
};

export default App;

weather.js weather.js

import React from 'react';
import PropTypes from 'prop-types';

const Weather = props =>
  <div>
    <p>{props.city}</p> 

        <p> humidity {props.humidity }</p> 

        <p> {props.description} </p>
    <p> temperature {props.temperature}</p> 
     <p> atmospheric pressure : {props.pressure}</p> 
 <p> atmospheric pressure : {props.rain}</p> 
 <img className="img-fluid" src={props.icon} />
  </div>





export default Weather; 

In order for you to display the icon, you need to use the URL and not just icon value. 为了显示图标,您需要使用URL,而不仅仅是图标值。 Something like this 像这样

The value returned from the API is '09d'. 从API返回的值是'09d'。 So you will have to append the URL with string url to have the weather api url appended with image and image extension. 因此,您将必须在URL后面附加字符串url,以将weather api url附加图像和图像扩展名。

{ http://openweathermap.org/img/w/${props.icon}.png } { http://openweathermap.org/img/w/${props.icon}.png }

在此处输入图片说明 Few other things noticed in your code is , 您的代码中几乎没有其他注意事项是,

You are setting default values as undefined, which is not correct. 您正在将默认值设置为undefined,这是不正确的。

Please use proper value, something like 请使用适当的值,例如

state = { temperature: '', city: '' } 州= {温度:”,城市:”}

https://codesandbox.io/s/6102m4kn3r https://codesandbox.io/s/6102m4kn3r

as per your code in weather.js 根据您在weather.js中的代码

<img src ={`http://openweathermap.org/img/w/${this.props.icon}.png`} alt="wthr img" />

it will display the weather icon...ie 它将显示天气图标...即

 import React, { Component } from 'react';
 class Weather extends Component {
    render() {
    return ( 
      <div>   
      <p>WeatherNow:
      <img src ={`http://openweathermap.org/img/w/${this.props.icon}.png`} 
         alt="wthr img" />
     </p>
        </div>

    );  
      }
      }

 export default Weather;

You have to make change in src attribute of <img> . 您必须更改<img> src属性。 As given in Weathermap api you have to request icon by http://openweathermap.org/img/w/10d.png . 如Weathermap api中所述,您必须通过http://openweathermap.org/img/w/10d.png来请求图标。 Replace 10d.png by props.icon+'.png' . 10d.png props.icon+'.png'替换10d.png It will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM