Results 1 to 3 of 3

Thread: Google Weather

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    11

    Google Weather

    Code:
    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
            With Grab_Weather
                .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
                        .Failed = True
                    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 = "http://www.google.com" & .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 = "http://wwww.google.com" & .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("/>"))
                    MsgBox(.current_icon)
                Catch ex As Exception
                    .Failed = True
                    .errormessage = ex
                End Try
                .Failed = False
            End With
            Grab_Weather = Nothing
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Grab_Weather("90078")
        End Sub

    Anyone know how I can display multiple "Weatherinfo" strings?

    Example: msgbox(.current_icon(1) & vbcrlf & .currenticon(2)) ... and so on.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Google Weather

    What you need is create a form or a custom usercontrol that contains all the necessary controls (picturebox for the icon, textboxes, labels... for the data) and after you grab the weather, send the data tthis form/usercontrol to display it. A messagebox will not let you display pictures. It's for displaying text only basically. And yes, you can display multiple lines of text by the use of the newline character. It's probably easier to contruct the message first using string.format function, and then display it. For example:
    Code:
    Dim msg As String = String.Format("{1}{0}{2}{0}{3}", Environment.NewLine, "line 1", "line 2", "line 3")
    MessageBox.Show(msg)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    11

    Re: Google Weather

    All I am parsing is the text, The rest I can add later on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width