Here’s another update. Change the city variable (city=my location) to the name of your city. Add whatever other cities you want to the table (name,latitude,longitude). Don’t forget to add the - sign where needed.
viewer.mode=STANDARD
function setup()
location.enable()
city="my location"
tab={
{city,location.latitude,location.longitude}, -- my location
{"London",51,0},
{"Sydney",-33,150},
{"New York",41,-74},
{"Seattle",47,-122},
{"Moscow",56,30},
{"Beijing",40,116},
{"North Pole",90,0},
{"South Pole",-90,0},
}
show=false
fOffset=1
if WIDTH>HEIGHT then
shx,shy=WIDTH-200,HEIGHT-75
else
shx,shy=WIDTH/4,HEIGHT/2+50
end
parameter.text("latitude")
parameter.text("longitude")
parameter.action("Use Keyed Data",req)
font("Courier")
fill(255)
APIKey = "7687f8ec79fcbe92cdd91a9589262a12"
req()
end
function req()
if latitude=="" then
latitude=location.latitude
longitude=location.longitude
else
city="Unknown name"
end
request()
showKeyboard()
hideKeyboard()
end
function draw()
background(0)
textMode(CORNER)
if lat~=nil then
text(city,100,HEIGHT-50)
text("Latitude: "..lat,10,HEIGHT-100)
text("Longitude: "..lon,10,HEIGHT-125)
text("Time Zone: "..tz,10,HEIGHT-150)
text("Time: "..tm,10,HEIGHT-175)
text("Summary: "..sm,10,HEIGHT-200)
text("Temperature: "..tp,10,HEIGHT-225)
text("Humidity: "..hm,10,HEIGHT-250)
text("Dew Point: "..dp,10,HEIGHT-275)
text("Pressure: "..pr,10,HEIGHT-300)
text("Wind Speed: "..ws,10,HEIGHT-325)
text("Wind Gust: "..wg,10,HEIGHT-350)
text("Wind Bearing: "..wb,10,HEIGHT-375)
text("UV Index: "..uv,10,HEIGHT-400)
text("Visibility: "..vs,10,HEIGHT-425)
end
showSelection()
end
function touched(t)
if t.state==BEGAN then
if t.x>shx-75 and t.x<shx+75 then
for z=1,#tab do
if t.y>shy-z*40-15 and t.y<shy-z*40+15 then
fOffset=z
latitude=tab[fOffset][2]
longitude=tab[fOffset][3]
city=tab[fOffset][1]
request()
end
end
end
end
end
function request()
if latitude~="" then
coord = string.format("%f,%f",latitude,longitude)
http.request("https://api.forecast.io/forecast/"..APIKey.."/"..coord,success)
end
end
function success(Data)
output.clear()
Data=string.gsub(Data,",","\
")
Data=string.gsub(Data,"{","\
{\
")
Data=string.gsub(Data,"}","\
}\
")
print(Data)
lat=string.match(Data,'latitude":(%g+)')
lon=string.match(Data,'longitude":(%g+)')
tz=string.match(Data,'timezone":(%g+)')
tm=string.match(Data,'time":(%g+)')
temp = os.date("*t", tm)
tm=string.format("%02d:%02d",temp.hour,temp.min).." last update"
sm=string.match(Data,'summary":(%g+)')
tp=string.match(Data,'temperature":(%g+)').." deg F"
dp=string.match(Data,'dewPoint":(%g+)').." deg F"
hm=(string.match(Data,'humidity":(%g+)')*100).." %"
pr=string.match(Data,'pressure":(%g+)')
pr=string.format("%4d mb %.2f in",pr//1,pr/33.86)
ws=string.match(Data,'windSpeed":(%g+)').." mph"
wg=string.match(Data,'windGust":(%g+)').." mph"
wb=string.match(Data,'windBearing":(%g+)').." deg"
uv=string.match(Data,'uvIndex":(%g+)')
vs=string.match(Data,'visibility":(%g+)').." miles"
end
function showSelection()
pushStyle()
rectMode(CENTER)
textMode(CENTER)
stroke(0, 237, 255, 255)
strokeWidth(3)
fill(255, 254, 0, 200)
rect(shx,shy,150,40)
fill(255, 0, 0, 255)
text("Location",shx,shy)
for z=1,#tab do
fill(47, 207, 122, 109)
rect(shx,shy-z*40,150,40)
fill(255)
text(tab[z][1],shx,shy-z*40)
end
popStyle()
end