By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

google weather api not working

P: 10
I need some help created a google weather api using vb .net.

I found the code online and there are no errors. Problem is that I can't seem to display wether information. Nothing shows up on screen and I am not sure what I did wrong. any ideas?



Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.Net
  3.  
  4. Partial Class UI_MasterPage
  5.     Inherits System.Web.UI.MasterPage
  6.  
  7.  
  8.     Protected Sub SearchIB_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles SearchIB.Click
  9.         Dim WeatherData As New Weather.WeatherInfo_Typed
  10.         WeatherData = Weather.Grab_Weather("albany")
  11.         temp.InnerHtml &= WeatherData.location & "--" & WeatherData.forcast_date & "--" & WeatherData.checked_time_date & "--" & WeatherData.humidity & "--" & WeatherData.forcast_date 
  12.     End Sub
  13. End Class
  14.  
  15.  
  16. <Runtime.CompilerServices.Extension()>
  17. Module Weather
  18.  
  19.     Public Structure WeatherInfo_Forecast
  20.         Dim DayOfWeek As String
  21.         Dim low As Double
  22.         Dim high As Double
  23.         Dim icon As String
  24.     End Structure
  25.  
  26.     Public Structure WeatherInfo_Wind
  27.         Dim direction As String
  28.         Dim speed As Double
  29.         Dim unit As String
  30.     End Structure
  31.  
  32.     Public Structure WeatherInfo_Typed
  33.         Dim Failed As Boolean
  34.         Dim errormessage As Exception
  35.         Dim location As String
  36.         Dim forcast_date As DateTime
  37.         Dim checked_time_date As DateTime
  38.         Dim humidity As Double
  39.         Dim highf As Double
  40.         Dim lowf As Double
  41.         Dim highc As Double
  42.         Dim lowc As Double
  43.         Dim currenttempC As Double
  44.         Dim currenttempF As Double
  45.         Dim predicted_icon As String
  46.         Dim current_icon As String
  47.         Dim current_condition As String
  48.         Dim predicted_condition As IEnumerable(Of WeatherInfo_Forecast)
  49.         Dim wind_condition As WeatherInfo_Wind
  50.         Dim day As String
  51.     End Structure
  52.  
  53.     <Runtime.CompilerServices.Extension()> _
  54.     Public Function ToC(ByVal F As Double) As Double
  55.         Return ((F - 32) / 9) * 5
  56.     End Function
  57.  
  58.     <Runtime.CompilerServices.Extension()> _
  59.     Public Function TryParseAsDouble(ByVal s As String) As Double
  60.         Dim rv As Double
  61.         If Double.TryParse(s, rv) = False Then rv = Double.NaN
  62.         Return rv
  63.     End Function
  64.  
  65.     <Runtime.CompilerServices.Extension()> _
  66.     Public Function TryParseAsDate(ByVal s As String) As DateTime
  67.         Dim rv As DateTime
  68.         If DateTime.TryParse(s, rv) = False Then rv = Nothing
  69.         Return rv
  70.     End Function
  71.  
  72.     Private Function ParseHumidity(ByVal s As String) As Double
  73.         If Not s Is Nothing Then
  74.             Dim humRegEx As New System.Text.RegularExpressions.Regex("Humidity: (?<Value>\d+)\w*\%")
  75.             Dim m = humRegEx.Match(s)
  76.             If m.Length = 0 Then Return Double.NaN
  77.             Return Double.Parse(m.Groups("Value").Value)
  78.         End If
  79.     End Function
  80.  
  81.     Private Function ParseWind(ByVal s As String) As WeatherInfo_Wind
  82.         Dim rv As New WeatherInfo_Wind
  83.         If Not s Is Nothing Then
  84.             Dim humRegEx As New System.Text.RegularExpressions.Regex("Wind\:\s+(?<Direction>[NEWSnews]{1,2})\s+at\s+(?<speed>(?<value>\d+)\s(?<units>\w+)){1}")
  85.             Dim m = humRegEx.Match(s)
  86.             rv.speed = Double.NaN
  87.             If m.Length = 0 Then Return rv
  88.             With rv
  89.                 .direction = m.Groups("Direction").Value
  90.                 If Double.TryParse(m.Groups("value").Value, .speed) = False Then .speed = Double.NaN
  91.                 .unit = m.Groups("units").Value
  92.             End With
  93.         End If
  94.         Return rv
  95.     End Function
  96.  
  97.  
  98.     Public Function Grab_Weather(ByVal Location As String) As WeatherInfo_Typed
  99.         Dim GrabWeather As New WeatherInfo_Typed
  100.         With GrabWeather
  101.             .Failed = True
  102.             Try
  103.                 Dim xml As XDocument = XDocument.Load("http://www.google.com/ig/api?weather=" & Location)
  104.                 Dim xp = xml.<problem_cause>
  105.                 If xp.Any Then Return GrabWeather
  106.                 .location = xml...<city>.@data
  107.                 .forcast_date = xml...<forecast_date>.@data.TryParseAsDate
  108.                 .checked_time_date = xml...<current_date_time>.@data.TryParseAsDate
  109.                 .humidity = ParseHumidity(xml...<humidity>.@data)
  110.                 .highf = xml...<high>.@data.TryParseAsDouble
  111.                 .lowf = xml...<low>.@data.TryParseAsDouble
  112.                 .highc = GrabWeather.highf.ToC
  113.                 .lowc = GrabWeather.highc.ToC
  114.                 .currenttempC = xml...<temp_c>.@data.TryParseAsDouble
  115.                 .currenttempF = xml...<temp_f>.@data.TryParseAsDouble
  116.                 '.current_icon = "http://www.google.com" & xml...<icon>.@data
  117.                 '.predicted_icon = "http://www.google.com" & xml...<high>.@data
  118.                 .current_condition = xml...<condition>.@data
  119.                 .predicted_condition = From f In xml...<forecast_conditions> _
  120.                                     Select New WeatherInfo_Forecast With { _
  121.                                       .DayOfWeek = f.<day_of_week>.Value, _
  122.                                       .high = f.<high>.@data.TryParseAsDouble, _
  123.                                       .low = f.<low>.@data.TryParseAsDouble}
  124.                 '.icon = "http://www.google.com" & f.<icon>.@data}
  125.                 .wind_condition = ParseWind(xml...<wind_condition>.@data)
  126.                 .day = xml...<day_of_week>.@data
  127.                 .Failed = False
  128.  
  129.                 Return GrabWeather
  130.             Catch ex As Exception
  131.                 .errormessage = ex
  132.  
  133.                 Return GrabWeather
  134.             End Try
  135.         End With
  136.     End Function
  137. End Module
  138.  
Jul 9 '15 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.