How I built a weather forecast app with location map in React & Leaflet! 
Tags: react, javascript, webdev, css
Published: 2020-09-01 07:12:46

---
title: How I built a weather forecast app with location map in React & Leaflet! 
published: true
description: Showcase of weather app and the challenges faced
tags: React, javascript, webdev, css
//cover_image: https://direct_url_to_image.jpg
---
Being a fledgling web developer, I wanted to grasp the concepts of React. Following tutorials is important, but than I thought why not make something you like . So I decided to make a weather forecast app in React (*sigh* another one). You've got to start somewhere, right.

# The app

Called WeatherMan React hosted on Github Pages, link to github profile [here](https://github.com/dsaw/weatherman-react). 
It is made in plain React (uses Context, no fancy Redux needed). Styling is done using Bootstrap and SASS. 
You can search any location from a huge list of over 100,000 places and fetch 7 day weather forecast. Additionally, I also added a location map shown in Leaflet where you can show layers made of temperature, cloudy etc.
Weather service used is [OpenWeatherMap](https://openweathermap.org/api) - It has a [one call API](https://openweathermap.org/api/one-call-api) which returns Hourly and Minutely forecast too.
Here's a little image snippet:

![WeatherMan React](https://dev-to-uploads.s3.amazonaws.com/i/uv7vl2vtpgpegsk0uidy.gif)
[https://dsaw.github.io/weatherman-react/](https://dsaw.github.io/weatherman-react/)

# Challenges faced
In the process of creating the site, there were hiccups.
- After the closing of Dark Sky API, there was no free weather API that gave hourly forecasts (24 hour each day or so). So I went with MetaWeather at first. It is a good, free API service that aggregates weather data from multiple websites. 
- After looking at other common websites, I used Algolia Search API for the autocomplete location service. The locations in Algolia and MW were different so I had to map it appropriately when clicking on the search term by setting Address context.
- But there were issues which I quickly encountered. There are many more locations in Algolia than there are in MetaWeather, which meant sometimes there were no equivalent places in MW giving weather results of neighbouring countries.
The time zones were incorrect for some places. 
To remedy, I switched to OpenWeatherMap which had ample data in the One Call API. It was decent enough.
- Lastly, I used Leaflet to render a map when location is selected. You can select layers like city forecast, rain, wind speed. I used a plugin named leaflet-openweathermap. It used JSONP to fetch requests which posed a problem when trying to make requests through the proxy, so had to refactor to use Fetch.
![Layered map](https://dev-to-uploads.s3.amazonaws.com/i/a07cyeza7ooavt1b7p61.PNG)

# TODO
To be honest, there are many improvements due:
- Refactor the code to separate presentation from the API specific parts
- Optimize the leaflet plugin code to improve response time
- Enhance Search bar & UI look & feel.

# Conclusion
It was a great experience to build something from the ground up. Taking the first step and pushing through to complete it is a satisfying feeling.
Please comment & hit me up if you have anything to talk about :)
You can check the project and leave any bugs/feature requests in here.
[https://github.com/dsaw/weatherman-react/issues](https://github.com/dsaw/weatherman-react/issues)