Springen naar inhoud


Tutorial info

  • Toegevoegd op: 31 May 2011 12:31
  • Bekeken: 1843
 


* * * * *
0 Beoordeling

VB.Net Weersvoorspelling uitlezen van google.

Geplaatst door pascalbianca op31 May 2011 12:31
Op het internet kom je en hoop API's tegen waarmee je de weersvoorspelling van bv. buienradar, google, enz. kunt uitlezen naar je site, maar sommige van ons zouden dat ook graag eens in een eigen programma willen zetten.

Nou hierbij de code die je kunt gebruiken om diverse weer gegevens te tonen van google in je programma zoals hieronder op de snapshot.



Geplaatste afbeelding

Maak een module aan met de naam.: Google_Weather

Plaats hierin de volgende code.:

Visual Basic Code:
Module Google_Weather
	Public Structure Weatherinfo
		Dim Failed As Boolean
		Dim errormessage As Exception
		Dim location As String
		Dim forcast_date As String
		Dim checked_time_date As String
		Dim humidity As String
		Dim highf As String
		Dim lowf As String
		Dim highc As String
		Dim lowc As String
		Dim currenttempC As String
		Dim currenttempF As String
		Dim predicted_icon As String
		Dim current_icon As String
		Dim current_condition As String
		Dim predicted_condition As String
		Dim wind_condition As String
		Dim day As String
	End Structure

	Public Function Grab_Weather(ByVal Location As String) As Weatherinfo
		Dim GrabWeather As New Weatherinfo

		With GrabWeather
			.Failed = True
			Try
				Dim webclient As New Net.WebClient
				Dim xml As String = webclient.DownloadString("http://www.google.com/ig/api?weather=" & Location)
				xml = xml.Replace(Chr(34), "").Replace(vbNewLine, "")

				If xml.Contains("<problem_cause data=/>") Then
					Return GrabWeather
				End If

				.location = Mid(Mid(xml, xml.IndexOf("<city data=") + 12), 1, Mid(xml, xml.IndexOf("<city data=") + 12).IndexOf("/>"))
				.forcast_date = Mid(Mid(xml, xml.IndexOf("<forecast_date data=") + 21), 1, Mid(xml, xml.IndexOf("<forecast_date data=") + 21).IndexOf("/>"))
				.checked_time_date = Mid(Mid(xml, xml.IndexOf("<current_date_time data=") + 25), 1, Mid(xml, xml.IndexOf("<current_date_time data=") + 25).IndexOf("/>"))
				.humidity = Mid(Mid(xml, xml.IndexOf("<humidity data=") + 16), 1, Mid(xml, xml.IndexOf("<humidity data=") + 16).IndexOf("/>"))
				.highf = Mid(Mid(xml, xml.IndexOf("<high data=") + 12), 1, Mid(xml, xml.IndexOf("<high data=") + 12).IndexOf("/>"))
				.lowf = Mid(Mid(xml, xml.IndexOf("<low data=") + 11), 1, Mid(xml, xml.IndexOf("<low data=") + 11).IndexOf("/>"))
				.highc = Math.Round((((.highf - "32") / "9") * "5"), 0)
				.lowc = Math.Round((((.lowf - "32") / "9") * "5"), 0)
				.currenttempC = Mid(Mid(xml, xml.IndexOf("<temp_c data=") + 14), 1, Mid(xml, xml.IndexOf("<temp_c data=") + 14).IndexOf("/>"))
				.currenttempF = Mid(Mid(xml, xml.IndexOf("<temp_f data=") + 14), 1, Mid(xml, xml.IndexOf("<temp_f data=") + 14).IndexOf("/>"))
				.current_icon = Mid(Mid(xml, xml.IndexOf("<icon data=") + 12), 1, Mid(xml, xml.IndexOf("<icon data=") + 12).IndexOf("/>")) : .current_icon = .current_icon
				.predicted_icon = Mid(xml, xml.IndexOf("<high data="))
				.predicted_icon = Mid(.predicted_icon, .predicted_icon.IndexOf("<icon data=") + 12)
				.predicted_icon = Mid(.predicted_icon, 1, .predicted_icon.IndexOf("/>"))
				.predicted_icon = .predicted_icon
				.current_condition = Mid(Mid(xml, xml.IndexOf("<condition data=") + 17), 1, Mid(xml, xml.IndexOf("<condition data=") + 17).IndexOf("/>"))
				.predicted_condition = Mid(xml, xml.IndexOf("<day_of_week data="))
				.predicted_condition = Mid(Mid(.predicted_condition, .predicted_condition.IndexOf("<condition data=") + 17), 1, Mid(.predicted_condition, .predicted_condition.IndexOf("<condition data=") + 16).IndexOf("/>") - 1)
				.wind_condition = Mid(Mid(xml, xml.IndexOf("<wind_condition data=") + 22), 1, Mid(xml, xml.IndexOf("<wind_condition data=") + 22).IndexOf("/>"))
				.day = Mid(Mid(xml, xml.IndexOf("<day_of_week data=") + 19), 1, Mid(xml, xml.IndexOf("<day_of_week data=") + 19).IndexOf("/>"))

				.Failed = False
				Return GrabWeather
			Catch ex As Exception
				.errormessage = ex
				Return GrabWeather
			End Try
		End With
	End Function

End Module


Maak een Form aan en plaats enkele labels, cq pictureboxen op zoals in bovenstaand voorbeeld en een Timer.

Plaats in je Form bv. de volgende code.:

Visual Basic Code:
Public Class VOORSPELLING
	Private Sub VOORSPELLING_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim WeatherData As New Google_Weather.Weatherinfo
		WeatherData = Google_Weather.Grab_Weather("Susteren")
		Label1.Text = WeatherData.lowc
		Label2.Text = WeatherData.highc
		Label3.Text = WeatherData.wind_condition

		Label4.Text = WeatherData.checked_time_date
		Label5.Text = WeatherData.current_condition

		PictureBox1.ImageLocation = WeatherData.current_icon

		Label7.Text = WeatherData.currenttempC
		Label8.Text = WeatherData.day
		Label9.Text = WeatherData.Failed

		Label10.Text = WeatherData.forcast_date
		Label11.Text = WeatherData.humidity
		Label12.Text = WeatherData.location

		Label13.Text = WeatherData.predicted_condition
		Label15.Text = WeatherData.wind_condition

		PictureBox2.ImageLocation = WeatherData.predicted_icon
		Label31.Text = "0"
		Timer1.Start()
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim WeatherData As New Google_Weather.Weatherinfo
		WeatherData = Google_Weather.Grab_Weather("Susteren")
		Label1.Text = WeatherData.lowc
		Label2.Text = WeatherData.highc
		Label3.Text = WeatherData.wind_condition

		Label4.Text = WeatherData.checked_time_date
		Label5.Text = WeatherData.current_condition

		PictureBox1.ImageLocation = WeatherData.current_icon

		Label7.Text = WeatherData.currenttempC
		Label8.Text = WeatherData.day
		Label9.Text = WeatherData.Failed

		Label10.Text = WeatherData.forcast_date
		Label11.Text = WeatherData.humidity
		Label12.Text = WeatherData.location

		Label13.Text = WeatherData.predicted_condition
		Label15.Text = WeatherData.wind_condition

		PictureBox2.ImageLocation = WeatherData.predicted_icon
	End Sub
End Class

Let op er zijn natuurlijke meerdere mogelijkheden, maar die mogen jullie zelf natuurlijk proberen, dit is alleen om jullie een aanzet te geven :)

Verander de locatie van je woonplaats of wat je wilt tonen in de volgende code regel.:

Visual Basic Code:
  WeatherData = Google_Weather.Grab_Weather("[b]Susteren[/b]")

Susteren moet dus veranderd worden in betreffende locatie :mellow:

Inloggen


Untitled 1

Hosting provided by Combell
Met dank aan Jürgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
vbib.be - vbnet.be - vbdotnet.be - visualbasic.be