Tomorrow.io's Resilience Platform is Here. Learn More.

X
Kelly Peters
By Kelly Peters
Kelly Peters
Kelly Peters
Currently Tomorrow.io's Director of Marketing, Kelly Peters is a growth-focused marketing leader with 10+ years of experience driving strategic communications and demand-focused initiatives for brands like CNBC, NBCUniversal, Dicks Sporting Goods, and JazzHR. At Tomorrow.io, she leads a dynamic team of growth and AI marketers in driving revenue with bold storytelling, radical creativity, and, above all else, human connection. Kelly graduated from Syracuse University and lives in Pittsburgh, PA.
Shahar Wider
Reviewed By Shahar Wider
Shahar Wider
Shahar Wider
As Director of Engineering at Tomorrow.io, Shahar Wider oversees the engineering group. In his role, he builds engineering-product-marketing capabilities and leads initiatives to scale the company's weather intelligence platform. With over 8 years of technical leadership experience, including 5 years in management roles, Shahar specializes in architecting innovative products powered by cutting-edge technologies. He believes in empowering teams to deliver robust solutions that drive business impact. Shahar holds a B.S. in Computer Science and enjoys staying on top of emerging tech trends.
Apr 5, 2024ยท 11 min, 2 sec

Building an App for Android Using a Weather API

    TL;DR:

    • Integrating a weather API into your Android app can provide numerous benefits for businesses and users.
    • Android is a cost-effective choice for development, and planning for mobile apps involves considerations like limited storage and data usage.
    • Utilizing a weather API effectively involves limiting calls, designing for various screen sizes, and tracking specific weather parameters.
    • Leveraging mobile GPS for localized weather updates and capturing events from severe weather conditions are key functionalities to consider.
    • By writing Kotlin code to make simple weather API calls, businesses can provide users with minute-to-minute updates on local weather conditions and enhance efficiency in various industries.

    With today’s technology, offering a seamless experience on smartphones is crucial. Prioritizing support for mobile platforms like Android can enhance your app’s value for both employees and users. To do so, you need a weather API.

    Integrating a weather API is a smart move that can provide numerous benefits. With a service like Tomorrow.io’s Weather API, you can effortlessly incorporate weather warnings, updates, tracking, forecasting, and other essential weather data into your application. While weather information might seem irrelevant for some business apps, it can actually offer significant advantages across various industries, making it a valuable addition to consider.

    โ€ŽWe could get into the numerous ways Android weather applications help businesses, but there are too many to count. Here are just a few examples:

    • Trucking and Supply Chain Optimization: Companies that rely on trucking for shipping products can use weather tracking to identify the safest and most efficient routes for their drivers, minimizing delays and ensuring timely deliveries.
    • Improved Travel Planning: Businesses with employees who travel frequently can utilize weather data to determine the best days for business meetings and optimizeย travelย times, reducing the risk of delays and enhancing overall productivity.
    • Government Preparedness:ย Governmentsย can leverage weather tracking applications to prepare for upcoming severe weather events, allowing them to take necessary precautions and allocate resources effectively.
    • Business Continuity Planning: Organizations located in areas prone toย severe weatherย can use weather apps to determine when they need to take action, such as closing offices to ensure employee safety and protect company assets during potentially catastrophic events.

    In this article, we will overview how to build an app for Android using the Tomorrow.io weather API.

    Planning and Designing a Weather Application for Android

    Choosing Android For Development

    Android is a primary choice for many organizations because itโ€™s an open-source operating system that 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.

    Challenges Specific to Mobile Apps

    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 a maximum of 256 GB. 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.

    Utilizing Weather APIs Effectively

    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 a business owns user phones, 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.

    Design Considerations For Various Screen Sizes

    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.

    Determining Which Weather Parameters to Track

    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.

    Leveraging Mobile GPS for Localized Weather Update

    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

    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 data 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 about their current location (if your application has permission to retrieve GPS location) and other locations where the user will be traveling.

    How to Capture 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.

    Events API

    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
    ย  ย  ย  ย  }
    ย  ย  ย  },
    ย  ย  ย  “…”
    ย  ย  ]
    ย  }
    }

    When integrating weather data into your Android application, it’s crucial to pay attention to the severity, certainty, and urgency values provided by the API. These three key metrics offer valuable insights into the potential impact of weather events on your users. Here’s a breakdown of what each value represents and how you can use them effectively:

    1. Severity: This value indicates the potential intensity of the weather conditions, such as the strength of a thunderstorm. By displaying the severity information, you can help users understand the level of threat they may face.
    2. Certainty: The certainty value represents the confidence level of the weather prediction. A higher certainty value means that the forecasted weather event is more likely to occur. Communicating this information to users allows them to make informed decisions based on the reliability of the data.
    3. Urgency: This value signifies how soon the weather event is expected to impact the user’s area. It helps users determine whether they need to take immediate action or if they have more time to prepare.

    Learn more in the Tomorrow.io Weather API documentation.ย 

    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.

     

    Sign Up for the Tomorrow.io Weather API for Free

    Try Now