Tomorrow.io’s 2022 ESG Impact Report is here. Download It Today.

X
Save your seat for ClimaCon 2022 now!

Building an App Using Weather API for Android

As more people use their smartphones for information, a benefit you can offer employees and your users is to focus application efforts on supporting mobile operating systems such as Android. You need a weather API to integrate weather tracking and forecasting, and an API like Tomorrow.io can help you easily integrate weather warnings, updates, tracking, forecasting, and other weather-related information. This might seem unnecessary in some business applications, but identifying weather conditions has numerous benefits across several industries.

We could get into the numerous ways Android weather applications help businesses, but there are too many to count. Supply chain businesses that rely on trucking to ship products could use weather tracking to find the safest and most efficient route for their drivers. Any business with traveling employees can help them determine the best days for business meetings and travel times. Governments can use weather tracking to prepare for upcoming severe weather. Businesses located in areas where severe weather can hinder productivity can use weather applications to determine when they need to prepare and close offices for the safety of employees and to protect assets during a catastrophic event.

Planning and Designing a Weather Application for Android

What makes Android a primary choice for many organizations is that it’s an open-source operating system and powers many of today’s applications. Android is a modified version of Linux, and embedded Linux is common in most electronic devices. It’s possible to port your project to iOS (Apple), but Android development is much more affordable for projects that must be tested on the market. If you already have an Android application, it makes sense to work with additional Android components for your new weather module. For applications with a large audience and user count, it might be beneficial to offer an Android and iOS version of your application, especially if you’re a business with a bring-your-own-device (BYOD) policy and want your employees to download the application.

You can either build your own application or integrate weather monitoring modules into your current mobile application. If you plan to build a new Android application using a weather API and you’ve never built one before, you should take several planning and design elements into consideration. Building mobile applications is much different than working with desktops, so we’ll cover a few challenges you might encounter.

Remember that mobile devices have limited computing resources, power, memory, and data speeds. A desktop might have several terabytes of storage capacity, but smartphones have not reached this level. Expensive Android smartphones have 1TB of storage, but most phones will have 256GB maximum. This means that storage is an issue, but the benefit of using a weather API is that all data is stored on the server backend and your application can call data as necessary without storing it on the local device.

The next challenge is limiting calls to the API while still making it data-rich. This might seem counterintuitive, but you must be aware that many of your users pay for data plans based on the amount of data transferred over a cellular signal. If user phones are owned by a business, then the business might not care if the application uses data aggressively. However, many individuals cap their data plan usage to reduce costs. If your application makes too many calls to the API, you could cost the user more money than expected. This challenge can be solved by limiting the data points returned in the JSON response to only the data that you need for the application.

One challenge you might already recognize is building your application to support the various screen sizes on smartphones and tablets. Android powers many smartphones, IoT devices, and tablets, and all of these screen sizes should be taken into consideration. Your Android designers and programmers work together to ensure that your application will work well with any screen size, but it must be a part of your planning and testing to ensure that the design supports all users and their devices.

A weather API collects data for all kinds of weather conditions, so you should also decide what you want to track. You might not need to track all weather conditions if you only need to display data for your local community. For example, wildfires are far more common in California than they are on the coast in Florida, so you can likely eliminate this data from your API calls if your users are located in Florida. By limiting the amount of data returned, you can reduce data usage on the user’s smartphone and avoid costing them money on excessive data transfers.

Although you have limited storage capacity, you can also cache some data to limit API calls. This might not be a viable solution for tracking future weather conditions, but it can be used to track historical weather conditions. The way you design and plan your weather application will dictate the way you design API calls and storage limits.

One advantage of using mobile devices for weather applications is the GPS option on most smartphones. If the user gives you permission on their smartphone to provide location information to the Android app, you can give them information about weather conditions in their area as they travel around or move from office to office. Note that your API account level determines the number of locations that can be added to your Tomorrow.io dashboard, so take this into account when you decide to build an Android application that dynamically creates location IDs to query weather data. Instead of using saved location IDs used in the following examples, you can use location points and coordinates to define the locations needed for API endpoint queries. 

Ready to get started? Explore Tomorrow.io’s 80+ data layers ->

Writing Kotlin Code to Make a Simple Weather API Call

Traditional Android programming began with simple Java language programming, but Kotlin is the currently preferred programming language for Android development. It can be used to start a new Android project or integrate new modules into an existing application. The Tomorrow.io API supports both Java and Kotlin calls, but we’ll focus on the Kotlin language to display examples.

One of the most basic data sets you can retrieve is a timeline of data for the next day. You can retrieve minute by minute day for 24 hours, or use different increments such as every 30 minutes or hour of data. A timeline of data can be useful for displaying weather information for the next day. It can be useful for employees traveling for work or a supply chain worker delivering products on a specific date. Severe weather conditions can affect these shipments, so your timeline query could be useful for logistics to reroute packages or plan for delays.

Here is a simple Kotlin call to the Timeline endpoint:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()  .url(“https://api.tomorrow.io/v4/timelines?location=607f3e4188a6a60007947b82&fields=temperature&units=imperial&timesteps=1h&apikey=YOUR_KEY”)
  .get()
  .addHeader(“Accept”, “application/json”)
  .build();

Response response = client.newCall(request).execute();

 

The above GET request makes a call to the weather API to get data points every hour for the next 24 hours. This request only returns temperature, but the Tomorrow.io API returns several other fields. The JSON data set looks like:

 

{
  “data”: {
    “timelines”: [
      {
        “timestep”: “1h”,
        “startTime”: “2022-02-14T21:00:00Z”,
        “endTime”: “2022-02-19T09:00:00Z”,
        “intervals”: [
          {
            “startTime”: “2022-02-14T21:00:00Z”,
            “values”: {
              “temperature”: 53.6
            }
          },
          {
            “startTime”: “2022-02-14T22:00:00Z”,
            “values”: {
              “temperature”: 53.13
            }
          },
          {
            “startTime”: “2022-02-14T23:00:00Z”,
            “values”: {
              “temperature”: 50.34
            }
          },
        (snipped for brevity)
        }
      ]
    }
}

 

The above JSON result is snipped for brevity, but the actual response would have 24 sets of values. The GET call indicates that the temperature values are in imperial, so this JSON result contains the temperature in Fahrenheit every hour for the next 24 hours.

Temperature isn’t the only data point you can get from a weather API, so you can choose to give the Android user a full weather report with predictions of future events. They can get information for their current location (if your application has permission to retrieve GPS location) and other locations where the user will be traveling.

Capturing Events from Severe Weather Conditions in Kotlin

The “Events” API endpoint is where you can collect data for severe weather conditions. As we mentioned, a weather tracking Android application has several benefits across numerous industries. There are two main endpoints to work with when you want to track severe weather: insights and events.

Think of insights as an endpoint that collects all the severe weather (or potential severe weather) that could affect your community. Like the document states, the data is collected from numerous organizations for better accuracy, and it can be used for “situational awareness.” You wouldn’t use the insights endpoint for basic temperature tracking, but you would use it for tracking potential fires, thunderstorms, blizzards or any other conditions that could threaten lives and harm businesses.

Events work directly with insights by letting you query for severe weather conditions within a specific geographic area. The geographic area is a location set in your Tomorrow.io account. You can also set a buffer to extend the radius around the specific location so that you can get event information on surrounding areas. For example, you might want to get information about storms in the surrounding area that could eventually travel to your location.

Here is a quick example of some Kotlin code that retrieves event data using thunderstorm insights:

OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder() .url(“https://api.tomorrow.io/v4/events?location=607f3e4188a6a60007947b82&insights=thunderstorms&buffer=1&apikey=YOUR_KEY”)
  .get()
  .addHeader(“Accept”, “application/json”)
  .build();

Response response = client.newCall(request).execute();

 

The location we used does not have any severe weather conditions, but here is an example of a dummy response that you would see if weather tracking organizations reported heavy thunderstorms in the area:

 

{
  “data”: {
    “events”: [
      {
        “insight”: “thunderstorms”,
        “startTime”: “2020-10-13T18:12:00Z”,
        “endTime”: “2020-10-14T15:06:26.261668224Z”,
        “updateTime”: “2020-10-13T18:12:00Z”,
        “severity”: “Unknown”,
        “certainty”: “Likely”,
        “urgency”: “Unknown”,
        “eventValues”: {
          “origin”: “VIIRS”,
          “title”: “Severe Thunderstorms”,
          “location”: {
            “type”: “Point”,
            “coordinates”: [
              130.191162109,
              47.230289459
            ]
          },
          “distance”: 0.156,
          “direction”: -7.481822
        }
      },
      {
        “insight”: “38f689d83c264eb0b084ba095f2ea772”,
        “startTime”: “2020-05-10T07:49:34+0000”,
        “endTime”: “2020-05-10T07:49:34+0000”,
        “eventValues”: {
          “precipitationIntensity”: 1,
          “mepIndex”: 300
        }
      },
      “…”
    ]
  }
}

 

It’s important to note the severity, certainty, and urgency values. You can get more information about these three values from the documentation, but it’s important to include code in your Android application to display warning information based on these three values. These values can tell users that severe weather conditions (in this example, thunderstorms) could be an immediate threat, or it can warn the user that severe conditions could potentially affect their area.

Where to Go from Here?

These examples are basic queries, but you can build an entire Android app that will provide users minute-to-minute updates on local weather conditions and use the GPS tracking data on the device to keep them updated as they travel. Using this strategy, you can help numerous industries build applications that protect their products, generate better efficiency for the supply chain, and help employees stay safe during severe weather.

Tomorrow.io's weather API offers a best-in-class interface with 80+ data layers to choose from historical, real-time, and forecast weather data.

More from Tomorrow.io

Register for ClimaCon 2021

ClimaCon is a completely free, digital conference. Register below.