Add current weather
This example populates your current weather information into text fields automatically based on your location.
This example assumes you've signed up for an API key from WeatherAPI.com. WeatherAPI.com has a free tier under 1M calls per month.
Here we're listening for the 'change-geometry'
event for a record, and then using the REQUEST function to make an API call to weatherapi.com. Once we get the response we parse it as JSON and use SETVALUE to update the form values listed here.
ON('change-geometry', function (event) {
const apiKey = 'PLACE API TOKEN HERE'
const location = `${LATITUDE()},${LONGITUDE()}`
const weatherUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${location}&aqi=no`
const options = {
url: weatherUrl,
};
REQUEST(options, function (error, response, body) {
if (error) {
ALERT('Error with request: ' + INSPECT(error));
} else {
var data = JSON.parse(body);
SETVALUE('current_temperature', data.current.temp_f)
SETVALUE('condition', data.current.condition.text)
SETVALUE('wind_mph', data.current.wind_mph)
SETVALUE('humidity', data.current.humidity)
SETVALUE('uv_index', data.current.uv)
}
})
});
This data event populates weather metrics, but many others are available under Realtime API and could be added as more fields in your app and add the SETVALUE to match.
NOTE: The LATITUDE and LONGITUDE functions will only return values if a record's geometry type is set to Point
, and there is a location set. Unless you have enabled Lines and Polygons for your app, the Point
type will be the only available geometry type for records in the app.
Updated 12 months ago