Create a .net 8 webapi project standby
Edit an entity class that is used to store the latitude and longitude data used to get the ip address
Continue editing the three classes to store specific weather packets corresponding to latitude and longitude:
Modify the default weather controller and write our own inside. For example, first write a request to get the IP's latitude and longitude coordinates
Run it and see what it looks like now. You can see the specific information about the ip that was obtained, including the operator, latitude and longitude coordinates, and so on.
Continue to expand the content to get weather data based on latitude and longitude
Run it and see some weather information
For reference, here is the specific json message returned:
{ "latitude": 22.5, "longitude": 114, "generationtime_ms": 0.05698204040527344, "utc_offset_seconds": 0, "timezone": "GMT", "timezone_abbreviation": "GMT", "elevation": 37, "current_weather_units": { "time": "iso8601", "interval": "seconds", "temperature": "°C", "windspeed": "km/h", "winddirection": "°", "is_day": "", "weathercode": "wmo code" }, "current_weather": { "time": "2024-10-11T09:45", "interval": 900, "temperature": 26.1, "windspeed": 6.6, "winddirection": 131, "is_day": 1, "weathercode": 2 } }
Data field parsing
Basic Information
- latitude: 22.5 - Latitude. Indicates the latitude location of the site.
- longitude: 114 - Longitude. Indicates the longitude location of the site.
- generationtime_ms: 0.05698204040527344 - Data generation time in milliseconds. Indicates the time taken from data request to data generation.
- utc_offset_seconds: 0 - Offset from UTC time in seconds. A 0 here means that the data is calculated in the GMT time zone.
- timezone: "GMT" - The timezone, expressed as GMT.
- timezone_abbreviation: "GMT" - timezone abbreviation, in this case also GMT.
- elevation: 37 - indicates that the elevation of the location is 37 meters above sea level.
Current Weather Units
The current_weather_units section is used to explain the units of each field in the current weather data.
- time: "iso8601" - the time format follows the ISO 8601 standard.
- interval: "seconds" - The time interval in seconds.
- temperature: "°C" - Temperature in degrees Celsius.
- windspeed: "km/h" - wind speed in kilometers per hour.
- winddirection:"°" - The wind direction unit is the angle, expressed in degrees.
- is_day: "" - This field has no units and is only used to indicate if it is daytime (usually 0 or 1).
- weathercode: "wmo code" - a code indicating weather conditions, using the WMO (World Meteorological Organization) standard code.
Current weather conditions
The current_weather section provides the actual weather data.
- time: "2024-10-11T09:45" - Indicates the specific point in time of the observation, in the format ISO 8601, i.e., October 11, 2024 at 09:45 p.m. Here we have to convert to our own Eighth East time, which, to add 8 hours, is 17:45 p.m.
- interval: 900 - indicates an observation interval of 900 seconds, or 15 minutes.
- temperature: 26.1 - the current temperature is 26.1°C.
- windspeed: 6.6 - Current wind speed is 6.6 km/h.
- winddirection: 131 - The current wind direction is 131 degrees. The wind direction is expressed in degrees, with 0 degrees meaning north, 90 degrees meaning east, and 131 degrees meaning roughly southeast.
- is_day: 1 - Indicates that the current observation is during the day, 1 means daytime, 0 usually means nighttime.
- weathercode: 2 - The current weather code is 2. 2 usually means "partly cloudy" according to the WMO weather code standard.
WMO Weather Codes Explained
According to the WMO (World Meteorological Organization) standards, the weathercode field provides specific information about the weather conditions. Below are some common WMO weather codes:
- 0: Sunny
- 1: Mainly clear
- 2: Partly cloudy
- 3: Cloudy
- 4: Cloudy
- 45: Foggy
- 48: Dense fog
- 51: Slightly hairy
- 53: Moderately hairy
- 55: Strong Gross Rain
- 61: Light showers
- 63: Moderate showers
- 65: Strong showers
- 71: Light snow
- 73: Moderate snow
- 75: Strong Snow
- 95: Thunderstorms with a slight chance of precipitation
- 99: Thunderstorms with heavy precipitation
Weather Description
In the current data, the weathercode is 2, which means that the weather conditions are "partly cloudy".
Get the online request code for latitude, longitude and weather:
LocationInfo locationInfo = null; using (var httpClient = _httpClientFactory.CreateClient()) { = (100); var res = ("/json/").GetAwaiter().GetResult(); (); var location = ().GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(location)) { locationInfo = <LocationInfo>(location); } } if (locationInfo != null) { using (var httpClient = _httpClientFactory.CreateClient()) { = (100); var res = ($"/v1/forecast?latitude={}&longitude={}¤t_weather=true").GetAwaiter().GetResult(); (); var weather = ().GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(weather)) { WeatherResponse weatherInfo = <WeatherResponse>(weather); return Ok(weatherInfo); } } }
If you need the full source code, you can reply "Weather Check" in the public number [Dotnet Dancer] to get the source code address.
The above is the whole content of this post, if it helps, welcome to like, in see, retweet share or comment, thank you for the big brother to support the scene~