Build a weather app with vanilla javascript

One of the best parts of JavaScript, can also be the worst. This is the undoubtedly simplistic ability to add an opening and closing script tag to the head of the HTML document and throw some spaghetti code in there. And voila! It works! Or does it? What is spaghetti code you ask? Spaghetti code is an unflattering term for code that is messy, has a tangled control structure, and is all over the place. It is nearly impossible to maintain and debug, usually has very poor structure, and is prone to errors.

So how do you quit writing this kind of code? In my humble opinion, you only have two options. Again, this is just my opinion. One, you use any of the umpteen number of JavaScript frameworks available at your disposal. Or two, you learn how to write JavaScript code with some sort of pattern or structure. The main difference between these patterns boils down to how the Data Layer, Presentation Layer, and Application Logic are handled.

Model Data Layer - This is where the data is stored for your app. The model is decoupled from the views and controllers and has deliberate ignorance from the wider context. Whenever a model changes, it will notify its observers that a change has occurred using an Event Dispatcher. You will read about the Event Dispatcher shortly.

In your To Do List App you will be building, the Model will hold the list of tasks and be responsible for any actions taken upon each task object. The view is also responsible for the presentation of the HTML. In your To Do List App, the view will be responsible for displaying the list of tasks to the user.

However, whenever a user enters in a new task through the input field, the view will use an Event Dispatcher to notify the controller, then the controller will update the model. This allows for swift decoupling of the view from the model.When I initially went through the coding bootcamp at The Iron Yard, Durham, I would always hear about these fun projects to work on and try out, but never had time to tackle.

This one is a simple weather application. If you happen to click on the link, you will need to search for a location to view weather. And how the information is retrieved and what information you have access to is completely dependent on which API you use.

I ended up using the API from the Weather Underground, mainly because it had fantastic documentation, was easy to use, and had some cool features, like webcams. If you exceed either, you will be disabled for the remainder of the day. This is one of those things that you really have to weigh your options when creating a weather app.

The other big drawback, is that the icons they provide for weather conditions were fairly grainy and pixelated. Other than those few minor things, I thought it was pretty great.

Build a simple Weather App with Node.js in just 16 lines of code

Because of the unappealing icons available, I ended up not using them very much. Instead, I created a database in Node, using MongoDB, where I stored high resolution images of different weather conditions. Then, I loop through my high res images, which I have tagged according to temperature, time of day and weather condition.

Once a match is found, I push it into an array and then pick a random image to use for the background of that forecast day, that matches the time day or night the weather condition and the temperature.

If you have any questions, feel free to reach out to me. Some of the APIs are quite limited, and others are incredible. Sign in. Ethan Jarrell Follow. Bursts of code to power through your day. Web Development articles, tutorials, and news. See responses 4. More From Medium. More from codeburst. CodeDraken in codeburst. Changhui Xu in codeburst. Ashan Fernando in codeburst. Discover Medium. Make Medium yours. Become a member.Last week I published an article on how to create a console weather app with Node.

This tutorial is somewhat of a continuation of of last weeks tutorial: Build a simple Weather App with Node. Before we begin, all the code for this project can be found in the GitHub Repo. Now that we have all the puzzle pieces, we can start building! First thing we need to do is get our server up and running. Express is a minimalist web framework for Node.

To use express, install it in the console:. Above is an example of the simplest application that we can create with Express. First we require the express package that was just installed.

Then, we create an instance named app by invoking Express. The app. We can test our server by running:. Now open your browser and visit: localhost and you should see Hello World!

Building a Weather App using Javascript

EJS is a templating language. In order to use EJS in Express, we need to set up our template engine:. A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page. This will make a lot more sense later in the tutorial.

We can then set up our template engine with this line of code just below our require statements in our server. EJS is accessed by default in the views directory. So create a new folder named views in your directory. Within that views folder, add a file named index. Think of our index. The html is just a form with one input for a city, and one submit button:. Once you have the above code copied into your index. The final thing we need to do is replace our app.

Instead, we want to send our index. Instead of using res. At this point, wee can test again by running:. Now open your browser and visit: localhost and you should see our index. Within that folder create a css folder, and finally create a file named style. Express wont allow access to this file by default, so we need to expose it with the following line of code:. Finally, we need our CSS.

By now, your server. We have one get route, and then we create our server. However, for our application to work, we need a post route as well.

If you look at our index. Now that we know where our form is posting, we can set up the route!

build a weather app with vanilla javascript

A post request looks just like a get request, with one minor change:.This is the first post in a multi-post tutorial! They actually have 11 different APIs all related to weather that you can access.

Build a realtime graph using JavaScript

Head on over to this link and sign up for an account. All you need is an email address and password. Once signed in, select the API keys tab. From Here you can Create a Key on the right hand side of the page. Enter a name anything works and select generate. Your API Key will appear on the left. Copy this key for later. Awesome, now that we have our API Key we can start creating our app! Fill out the required information to initialize our project. Create a file named index. Now that we have all the puzzle pieces, we can start building!

Install request by running:. As I said, request is pretty easy to use. We just need to pass in our target url, and request returns a callback function. Our starter code looks like this:. Lets break this code down:. We can now run our code in the console by typing:. It works! In future tutorials I will show you how to hide your API key using environment variables. For now, just know that it is not standard to have your API key exposed like this. Lets clean everything up. JavaScript Object Notation is a way to store information in an organized and easy to access manner — and it looks like a JavaScript Object, but if you noticed, there are some extra quotation marks involved.

We can make this conversion with one line of code:. Now that we have a JavaScript object, we can access data within the object with dot or bracket notation.

build a weather app with vanilla javascript

Below, we construct a message string by accessing data within our weather object:. So we need to add another query parameter. Our app is still boring. We can only access the weather for Portland, Oregon. Lets add some interactivity. Yargs is a pirate themed interactive command line interface tool.There are a lots of weather application available in Google Play Store.

Bt you may want to develop your own weather application in Android Studio. There are many websites you can get by searching on the Google which supply free weather Api, but In this tutorial we are going to use a Api provided by OpenWeatherMap. Though today our task is to create an android weather app but throughout this tutorial we'll learn something new if you didn't know them already.

Create a new Android Studio project. For this tutorial, I used the name of the project "Weather App". Before proceeding please get an API key by registering. You can either follow the attached video above or steps below. Apps that need to access Internet must request Internet permission. You can request for internet permission in your AndroidManifest.

In the next part, we'll implement this custom style. As I mentioned above that we'll use a custom theme to disable the default ActionBar, we can use Theme. NoActionBar to implement the theme.

Also I'll set the default text color to White so that I need not write the text color in all views later. Please remember that I only did this for my design purpose. If you need the default action bar you shouldn't do that. Also avoid adding the custom theme into your AndroidManifest. You have already noticed from the cover photo of this article that I'll use a gradient background in this weather app tutorial. But we'll not use any image for this gradient background.

Rather this can be implemented using a Drawable Resource file. Then select Drawble as Resource type. Now for our designing purpose we'll need to use some icons like for humidity, pressure, sunrise, sunset etc. Download the images I used for this tutorial. You can visit Github to the library url to check if you are always using the latest version.

The time I'm writing this article, version 1. In future, if I find any bugs, I'll update it and you should use the latest one.

Build A Weather App With Vanilla Javascript Tutorial | Javascript For Beginners

As I suggested before, use the latest version always. Check for the Library Page to get the latest one. Now we'll need to write code to grab the weather information from the API. First I'm providing the full code of my MainActivity.

AsyncTask enables proper and easy use of the UI thread. As you can see in our AsyncTask named as " weatherTask ", we displayed the ProgressBar in onPreExecute so that user can understand that the app is making request to the url for getting weather information. Both of them are declared as:.

I've already posted an article on Getting Current Location latitude, longitude in Android using Kotlin. Then the response from the url will be passed to the onPostExecute Result. As the api passes weather information in JSON format, we'll extract the data so that we can later set the extracted data into our views.The first official weather forecast ever appeared on the first of August The legendary vice-admiral Robert Fitzroy, a royal-navy officer and pioneer in meteorology, was collecting data for his Meteorological Office from 15 land stations across England.

Nowadays, there are over stations across our planet, accessible via free, affordable, advanced, and accurate technologies. So what are we waiting for? An API application programming interface is a form of pre-defined interaction between the client and the service.

Applications are only limited by our imagination and creativity. You can embed it inside your app as a feature all data is ready, there is no need to process it. You can make a bot for some messenger that will return a forecast.

There are many popular weather APIs out there to choose from. Their weather database is huge. You can use diverse metrics e. You can also get a simple forecast, daily forecast, historical weather data, or use a city search. This weather API is gaining popularity.

It is a freemium-based API with powerful endpoints :. As mentioned above, the list is hugeso you can go with something else. For installing node. Once you have installed the node and npm to your PC, you can easily check it by typing the following commands in the terminal:. First, create a folder where you want to keep all the work. Then move it in the terminal you can use the cd command and type this line:. Finally, there will be a package. An example of the syntax for installing any package looks like this:: npm install.

So, for our modules, it should look like this:. Express is a lightweight framework for node. Unirest is a simple and powerful solution that requests a library. For more information, visit this doc. It allows anybody to use them via a straightforward interface. Navigate to Endpoints, or click here.

The structure looks pretty understandable:. The left section of the screen consists of links with all the unique endpoints in the API. We will come back to this soon to explain all the features of the OpenWeatherMap. The middle column gives detailed info about the selected endpoint i.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. The icons we're going to use for our web application are created by the Graphic Designer Ashley Jager. Skip to content.

How To Build a Weather App in JavaScript (Node.js) Using the OpenWeatherMap API

Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up.

CSS Branch: master. Find file.

build a weather app with vanilla javascript

Sign in Sign up. Go back. Launching Xcode If nothing happens, download Xcode and try again. Latest commit. Latest commit 7e8bed5 Jul 3, Hi Everyone! When the user clicks on the temperature value, that will convert it from Celsius to Fahrenheit.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Add files via upload. May 18, Update app. May 19, Jul 3,


Leave a Reply

Your email address will not be published. Required fields are marked *