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

  • OpenWeather.zip
    11.4 KB · Views: 466

GMan

Well-Known Member
Licensed User
Longtime User
I got an error and the App crashes when starting.
The log tells me:
Sub jobdone was not found
 

JCO

Active Member
Licensed User
Longtime User
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?
 

GMan

Well-Known Member
Licensed User
Longtime User
Yes, 6.80
 

JCO

Active Member
Licensed User
Longtime User
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

  • OpenWeatherB4A-LT7.zip
    11.6 KB · Views: 330

GMan

Well-Known Member
Licensed User
Longtime User
Thx a lot - this works like a charm :):D
 

GMan

Well-Known Member
Licensed User
Longtime User
Is it possible to work with Lat/Lon - values, i.e. for getting the weather at the actual position OR a given coordinate ?
 

JCO

Active Member
Licensed User
Longtime User
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.
 

GMan

Well-Known Member
Licensed User
Longtime User
OK - hope you have "some" time (soon) :rolleyes:
 

GMan

Well-Known Member
Licensed User
Longtime User
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.

B4X:
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
 

JCO

Active Member
Licensed User
Longtime User
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:
B4X:
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
 
Top