Weather Application using JSON API Response
Shows you the weather for your local area.
Simplistic Dark Theme Responsive Design
[VB] Visual Basic Projects - Weather Viewer Via JSON API
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | Imports System.Web.Script.Serialization Imports System.IO Imports System.Net Public Class Form1 'Location Data Public lat As String = My.Settings.Latitude Public log As String = My.Settings.Longitude 'Retrieve location data via ip address Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load RefreshLocationData() If My.Settings.Latitude = Nothing And My.Settings.Longitude = Nothing Then Try 'Query location data Dim Req As HttpWebRequest Dim Ret As HttpWebResponse = Nothing Dim SR As StreamReader Req = DirectCast(WebRequest.Create("http://ip-api.com/json"), HttpWebRequest) Ret = DirectCast(Req.GetResponse(), HttpWebResponse) SR = New StreamReader(Ret.GetResponseStream()) Dim Raw As String = Nothing Raw = SR.ReadToEnd() Dim JavaScriptSerialization As New JavaScriptSerializer() Dim ipdata_object As New IPData() ipdata_object = JavaScriptSerialization.Deserialize(Raw, ipdata_object.GetType) lat = ipdata_object.lat log = ipdata_object.lon My.Settings.Latitude = lat My.Settings.Longitude = log My.Settings.Save() Catch ex As Exception MessageBox.Show(ex.ToString()) End Try Else lat = My.Settings.Latitude log = My.Settings.Longitude End If CollectWeatherData(lat, log) End Sub 'generate your own free api key at openweathermap.org to use with your own project. Public api_key As String = "&APPID=a756bb2cdd548defd8539e8e36c1d2df" Public api_measurement As String = "imperial" Public weather_url As String = "http://api.openweathermap.org/data/2.5/weather?lat=" + My.Settings.Latitude + "&lon=" + My.Settings.Longitude + "&APPID=a756bb2cdd548defd8539e8e36c1d2df&units=imperial" 'Refresh Location Data Public Sub RefreshLocationData() ListBox2.Items.Clear() 'Query location data Dim Req As HttpWebRequest Dim Ret As HttpWebResponse = Nothing Dim SR As StreamReader Req = DirectCast(WebRequest.Create("http://ip-api.com/json"), HttpWebRequest) Ret = DirectCast(Req.GetResponse(), HttpWebResponse) SR = New StreamReader(Ret.GetResponseStream()) Dim Raw As String = Nothing Raw = SR.ReadToEnd() Dim JavaScriptSerialization As New JavaScriptSerializer() Dim ipdata_object As New IPData() ipdata_object = JavaScriptSerialization.Deserialize(Raw, ipdata_object.GetType) ListBox2.Items.Add("----- Coordinates -----") ListBox2.Items.Add("Latitude: " + ipdata_object.lat) ListBox2.Items.Add("Longitude: " + ipdata_object.lon) ListBox2.Items.Add("") ListBox2.Items.Add("----- Location -----") ListBox2.Items.Add("Country: " + ipdata_object.country) ListBox2.Items.Add("City: " + ipdata_object.city) ListBox2.Items.Add("Zip Code: " + ipdata_object.zip) ListBox2.Items.Add("") ListBox2.Items.Add("----- User Data -----") ListBox2.Items.Add("ISP: " + ipdata_object.isp) ListBox2.Items.Add("ORG: " + ipdata_object.org) ListBox2.Items.Add("Timezone: " + ipdata_object.timezone) ListBox2.Items.Add("IP: " + ipdata_object.query) ListBox2.Items.Add("") ListBox2.Items.Add("----- Region Data -----") ListBox2.Items.Add("Country Code: " + ipdata_object.countryCode) ListBox2.Items.Add("Region Name: " + ipdata_object.regionName) ListBox2.Items.Add("Region: " + ipdata_object.region) End Sub 'collect data Public Sub CollectWeatherData(latitude As String, longitude As String) Try 'Reset application ListBox1.Items.Clear() 'Query weather data Dim Req As HttpWebRequest Dim Ret As HttpWebResponse = Nothing Dim SR As StreamReader Req = DirectCast(WebRequest.Create(weather_url), HttpWebRequest) Ret = DirectCast(Req.GetResponse(), HttpWebResponse) SR = New StreamReader(Ret.GetResponseStream()) Dim Raw As String = Nothing Raw = SR.ReadToEnd() Dim JavaScriptSerialization As New JavaScriptSerializer() Dim weatherdata As New WeatherResponse() weatherdata = JavaScriptSerialization.Deserialize(Raw, weatherdata.GetType) 'populate the labels with data Label1.Text = "Weather: " + weatherdata.weather(0).main Label4.Text = "Geographic Coordinates: " + lat + " , " + log Label5.Text = "Tempurature: " + weatherdata.main.temp.ToString() + "°" Label6.Text = "Tempurature Forecast: " + "High " + weatherdata.main.temp_max.ToString() + "°" + " / " + "Low " + weatherdata.main.temp_min.ToString() + "°" 'set application icon Me.Icon = DirectCast(My.Resources.ResourceManager.GetObject("_" + weatherdata.weather(0).icon.ToString()), Icon) 'Populate raw weather data ListBox1.Items.Add("----- Weather Data -----") ListBox1.Items.Add("Forecast: " + weatherdata.weather(0).main) ListBox1.Items.Add("Tempurature: " + weatherdata.main.temp + "° F") ListBox1.Items.Add("Wind Speed: " + weatherdata.wind.speed + " mph") ListBox1.Items.Add("Wind Direction: " + weatherdata.wind.deg + "°") ListBox1.Items.Add("") ListBox1.Items.Add("----- Systemic Data -----") ListBox1.Items.Add("Weather ID: " + weatherdata.weather(0).id) ListBox1.Items.Add("Weather Icon: " + weatherdata.weather(0).icon) ListBox1.Items.Add("Country Code: " + weatherdata.sys.country) ListBox1.Items.Add("Sunrise Data: " + weatherdata.sys.sunrise) ListBox1.Items.Add("Sunset Data: " + weatherdata.sys.sunset) ListBox1.Items.Add("Response ID: " + weatherdata.id) Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub End Class 'IP Data API Deserialization classes for http://ip-api.com/json API response <Serializable> Class IPData Public asinfo As String Public city As String Public country As String Public countryCode As String Public isp As String Public lat As String Public lon As String Public org As String Public query As String Public region As String Public regionName As String Public status As String Public timezone As String Public zip As String End Class 'Weather API Deserialization classes for openweathermap.org API response <Serializable> Class WeatherResponse Public coord As CoordinateInfo Public weather As WeatherInfo() Public base As String Public main As MainInfo Public visibility As String Public wind As WindInfo Public clouds As CloudInfo Public dt As String Public sys As SysInfo Public id As String Public name As String 'Location Name Public cod As String End Class <SerializableAttribute> Class CoordinateInfo Public lon As String Public lat As String End Class <SerializableAttribute> Class WeatherInfo Public id As String Public main As String 'Weather Type Public description As String 'Weather Description Public icon As String End Class <SerializableAttribute> Class MainInfo Public temp As String Public pressure As String Public humidity As String Public temp_min As String Public temp_max As String End Class <SerializableAttribute> Class WindInfo Public speed As String 'Wind Speed Public deg As String 'Wind Heading End Class <SerializableAttribute> Class CloudInfo Public all As String End Class <SerializableAttribute> Class SysInfo Public type As String Public id As String Public message As String Public country As String Public sunrise As String Public sunset As String End Class |