#Geo - Get Current Weather for User Location
CompletedWe have several use case scenarios where we need current weather attributes (temperature, wind speed and direction, etc.) for the weather station closest to the user's location (occupied point) embedded into our forms. There are several paid services available to accomplish this (and I've built them as well), but we opted to use NOAA's (National Oceanic and Atmoshpheric Administration) api directly as it is available to the public, does not require credentials and free.
One of the issues we ran into is that NOAA's web service is very slow (I'll sideline any discussion over this for now), so we opted to grab everything we needed in a single web service call and concatenate the attributes all together for later parsing, rather than making multiple calls. Follow these steps to add this:
1) Place the code in the link below into your page-level Javascript:
2) Add a Location widget to your form
Data Column Name: my_location
Dynamic Value: {""}
3) Add a Text Area element to your form to hold the string for parsing
Data Column Name: all_weather
Dynamic Value: {getCurrentWeather(my_location.latitude, my_location.longitude)}
Condition Value: false
Reference ID 1: (on the element): ELEMENT_SKIP_REPORT
4) Add the following Read Only elements and values your form (for clarification, Read Only element type, not a text element with read-only checked):
Current Weather
Dynamic Value: {(all_weather.split("||"))[0]}
Temperature (F)
Dynamic Value: {(all_weather.split("||"))[1]}
Wind Speed (mph)
Dynamic Value: {(all_weather.split("||"))[2]}
Wind Direction (degrees)
Dynamic Value: {(all_weather.split("||"))[3]}
Wind Gust (mph)
Dynamic Value: {(all_weather.split("||"))[4]}
Relative Humidity (%)
Dynamic Value: {(all_weather.split("||"))[5]}
Pressure
Dynamic Value: {(all_weather.split("||"))[6]}
Wind Chill
Dynamic Value: {(all_weather.split("||"))[9]}
Condition Value: (all_weather.split("||"))[1]<=32 && (all_weather.split("||"))[2]>=5
Forecast
Dynamic Value: {(all_weather.split("||"))[10] + ": " + (all_weather.split("||"))[11]}
Weather Station:
Dynamic Value: {(all_weather.split("||"))[7]}
Weather Station Elevation (ft):
Dynamic Value: {(all_weather.split("||"))[7]}
Please feel free to comment and improve, or ask questions.
Thanks!
-
Official comment
Hi Folks,
We have implemented this functionality on Android in the 8.9 release so users are able to call Get and Post functions using xmlhttp. https://iformbuilder.zendesk.com/hc/en-us/articles/360027972491-Android-Release-Notes-8-9
Thanks!
-Ben
Comment actions -
JACOB:
Excellent and incredibly useful function - thanks for sharing! Just to note, there are a few very minor inconsistencies/errors in your example that people should be aware of to make sure they get it to work:
1. They should double-check "my_location" vs. "mylocation" and "all_weather" vs. "allweather" after pasting the text as Dynamic Values or Conditional Values (whether or not their column name uses an underscore - it varies in the example text provided).
2. Weather Station Elevation (ft) should reference column [8] not [7].
3. There are a couple of hard-returns (empty lines) in the page-level script that should be deleted after pasting, otherwise the script won't run.We will definitely get some use out of this wonderful function - thanks again!
Will -
Jacob - Quick question. This is a fantastic function. Could you clarify when exactly the "current" weather data is captured when a GPS location is captured offline? Does it just grab the data when connection is regained, or when the device syncs? Or does it grab historical data (the weather when the device was offline an the GPS collected)? If it doesn't grab the older weather data - any ideas on how to tie it to when the GPS location was collected (either by using the date/time in the GPS data or some other way? Thanks a lot.
Will -
Hi Will,
This example is currently set up to require an internet connection when the form is being filled out, as that is a requirement for some of the work our users are doing in the field.
In cases where we do not need it real-time, we call NOAA's xml api for historic data, based on the user's occupied point and date-time stamp. We do some of this today. We generally make this call from our servers once the data is back inside our borders, so a slightly different workflow.
-
The underscores in my post were omitted too - that explains it! Let me know if you have any ideas on the NOAA (or other weather web services) historical data. I just realized the GPS location only collects time (not date) so another element (or metadata time/date) would need to be used...
W -
Hi Jacob - looks great - Any thoughts why it doesn't work on Android - see discussion re this on this thread.
-
Hi Guys, I know this is a very late reply and we apologize, but for those folks who were struggling with making API calls work on Android, unfortunately Android JS library doesn't support XmlHttpRequest. Android JS is handled by Android webkit and you will be able to find all the classes supported: https://developer.android.com/reference/android/webkit/package-summary.html Let me know if you have any questions!
Penny
-
Hi Robert, please make sure that you have line 34 from the snippet included in the page level JS, which includes a request header.
https://gist.github.com/truth3/48a5117310a5ce1bc7c41ced27e77d0d
Please sign in to leave a comment.
Comments
13 comments