B4A Class OpenWeatherMap class

I'd like to share a class I just created for (hopefully easily) accessing OpenWeatherMap API weather information.
Its use is pretty much straightforward and has comments in the appropriate calls, but feel free to ask a question if it's not as clear as I think it is ;)

Attached is the class and a small sample project
 

Attachments

I got an error and the App crashes when starting.
The log tells me:
Sub jobdone was not found
 
It relies on the Resumable Subs feature.
From the error, my guess is that the problem's there.
Are you using a B4A version earlier than 7.0?
 
Yes, 6.80
 
OK, it was easier than I thought. Here's a version that should work on 6.80. However, I can't test it because I removed the older B4A versions from my PC.
 

Attachments

Thx a lot - this works like a charm :):D
 
Is it possible to work with Lat/Lon - values, i.e. for getting the weather at the actual position OR a given coordinate ?
 
It is possible with their API, but I haven't implemented it yet in the class.
I am thinking about doing a 2nd version which would include that possibility, but I don't know when i'll have the time to get it done.
 
OK - hope you have "some" time (soon) :rolleyes:
 
As you are using auto-generated labels with auto-generated names, i make all your labels invisible and catch the depending value and display it in my labels.

Code: Copy
Sub ShowWeather()
    Dim currentY As Int = 0
    
    'lblHdr1.Text = wt.GetValue("main")
    WetterLabel.Text = wt.GetValue("main")
    'lblHdr2.Text = wt.GetValue("temp") & wt.GetUnit("temp")
    wt.LoadCurrentIcon(imgWeather)
    wt.LoadCurrentIcon(WettersymbolImageView)
    TempAussenLabel.Text = wt.GetValue("temp") & wt.GetUnit("temp")
    Dim lbl As Label
    lbl.Initialize("")
    lbl.TextSize = 12
    Dim s As String = wt.GetValue(lstDetails.Get(0))
    s = s.SubString2(0, 1).ToUpperCase & s.SubString(1)
    lbl.Text = s
    WetterPanel.AddView(lbl, 1dip, currentY, 100%x, 140dip)
    currentY = currentY + 15dip
    Dim theKey As String
    For i = 1 To lstDetails.Size-1
        theKey = lstDetails.Get(i)
        If wt.GetValue(theKey) <> "" Then
            Dim lbl As Label
            lbl.Initialize("")
            lbl.Visible=False ' Unsichtbar machen
            lbl.TextSize = 10
            lbl.Text = wt.GetCaption(theKey) & ": " & wt.GetValue(theKey) & wt.GetUnit(theKey)
            If theKey = "wind_speed" Then
                lbl.Text = lbl.Text & " (" & NumberFormat2(wt.GetValue(theKey) * 3.6, 1, 2, 1, False) & " km/h)"
                WindSpeedLabel.Text = NumberFormat2(wt.GetValue(theKey) * 3.6, 1, 2, 1, False)
            End If
            If theKey = "wind_degrees" Then
                lbl.Text = lbl.Text & " (" & wt.GetValue("cardinal") & ")"
                WindrichtungLabel.Text = wt.GetValue("cardinal")
            End If
            If theKey = "pressure" Then
                LuftdruckLabel.Text = wt.GetValue("pressure")
            End If
            If theKey = "humidity" Then
                HumAussenLabel.Text = wt.GetValue("humidity")
            End If
            WetterPanel.AddView(lbl, 1dip, currentY, 100%x, 140dip)
            currentY = currentY + 15dip
        End If
    Next
    
End Sub
 
That part is just an example on the use of the class.
If you want specific items in labels added with the designer, you only need to get those, no need to do the loop or anything else. Like:
Code: Copy
Sub ShowWeather()
    WetterLabel.Text = wt.GetValue("main")
    wt.LoadCurrentIcon(WettersymbolImageView)
    TempAussenLabel.Text = wt.GetValue("temp") & wt.GetUnit("temp")
    WindSpeedLabel.Text = NumberFormat2(wt.GetValue(theKey) * 3.6, 1, 2, 1, False)
    WindrichtungLabel.Text = wt.GetValue("cardinal")
    LuftdruckLabel.Text = wt.GetValue("pressure") 'Although here, I would add  & wt.GetUnit("pressure")
    HumAussenLabel.Text = wt.GetValue("humidity")
    
End Sub
 
Back
Top