Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Make a Web-based Weather Report of your Location using OpenWeatherMap API
Next article icon

Make a Web-based Weather Report of your Location using OpenWeatherMap API

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Follow these simple steps in order to make a Web-based Weather Application Using OpenWeatherMap API.

Step 1: Make your account in OpenWeatherMap for accessing their API for our project. Create an Account. It's totally FREE. After making an account you will get a default key just note/copy that key because we will use it for our feature steps.

Step 2: Write down the following HTML code in the MainPage.html file. You can choose any name for the file.

Filename: MainPage.html

HTML
<!DOCTYPE html> <html>  <head>     <title>Weather Report</title>     <link rel="icon" href="favicon.png">     <link rel="stylesheet" href="PageStyle.css">     <link href= 'https://fonts.googleapis.com/css?family=Delius Swash Caps'            rel='stylesheet'> </head>  <body>     <p id="data" class="styleIt"></p>      <script src="JSPage.js"></script> </body>  </html> 

Explanation:

  • The 5th line is used to put the favicon icon on your HTML page.
  • The 6th line is used to link our CSS file to the HTML file.
  • The 7th line is used for using a font that is taken from the web.
  • The 10th line is used for printing the data which we get from JS.
  • The 12th line is used to link our JS file to the HTML file.

Step 3: Write down the following CSS for making the looks of the web-page more attractive.

Screenshot1260


Filename: PageStyle.css

HTML
p.styleIt{     background-color: rgb(182, 182, 182);     border: 2px solid rgb(182, 182, 182);     border-radius: 8px;     text-align: center;     box-shadow: 6px 5px 2px rgb(182, 182, 182),      0 0 25px rgb(0, 0, 0), 0 0 5px rgb(182, 182, 182);     font-family: 'Delius Swash Caps'; }  body{     background:rgb(120, 120, 120);     margin: 0;     position: absolute;     top: 50%;     left: 50%;     margin-right: -50%;     transform: translate(-50%, -50%) } 

Explanation:

  • We have used p.styleIt where p represents the HTML tag of the paragraph and the .styleIt is used to target the particular paragraph which was called by class="styleIt" on the above code (Step3).
  • To give a shadow looks to the paragraph we have used box-shadow: 6px 5px 2px rgb(182, 182, 182), 0 0 25px rgb(0, 0, 0), 0 0 5px rgb(182, 182, 182);
  • To make a carved border we have used border-radius: 8px;
  • To align the whole body at the center of the web-page:- the body{.....} is used.
With Out CSS
Application Without CSS
with CSS
Application With CSS

Step 4: This is the main file of our whole project which will bring the Location of our present location and using API get the data of our location and print in the web-page.

Filename: JSPage.js

JavaScript
var data = document.getElementById("data"); var Latitude; var Longitude; var key = "------Put Your Own Key-----"; var url = "http://api.openweathermap.org/data/2.5/weather?";  // Function to get the latitude and longitude data function getLocation() {     if (navigator.geolocation) {         navigator.geolocation.getCurrentPosition(showPosition);     } else {         data_of_Lat_Lon.innerHTML =              "Geolocation is not supported by this browser";     } }  // Function to fetch the Latitude and Longitude // from position data function showPosition(position) {     Latitude = position.coords.latitude;     Longitude = position.coords.longitude;      getData(Latitude, Longitude); }  // Fetching the data and calling the API function getData(Lat, Lon) {     const readyToSent = (url + "lat=" + Lat          + "&lon=" + Lon + "&appid=" + key);     fetch(readyToSent)         .then(response => response.json())         .then(data => {             console.log(data);             fetchData(data)         }) }  // Fetching the JSON file and printing it to  // the paragraph which is called by ID data function fetchData(data) {     const icon = "http://openweathermap.org/img/wn/"         + data.weather[0].icon + "@2x.png"      document.getElementById("data").innerHTML =         "<b>The weather report of your Location is :-"             + "</b><br> <img src=" + icon + "><br>"             + "<b>Country :</b>" + data.sys.country              + "<br><b>Local Area Name :</b>"              + data.name + "<br><b>Temp. :</b>"              + parseFloat((data.main.temp - 273.15))             .toFixed(1) + "℃" +              "<br><b>But You will feel like :</b>"              + parseFloat((data.main.feels_like -                  273.15)).toFixed(1) + "℃"              + "<br><b>Min. Temp. :</b>"              + parseFloat((data.main.temp_min -                  273.15)).toFixed(1) + "℃"              + "<br><b>Max. Temp. :</b>"              + parseFloat((data.main.temp_max -                  273.15)).toFixed(1) + "℃"              + "<br><b>Pressure :</b>"              + data.main.pressure + "hPa"              + "<br><b>Humidity :</b>"              + data.main.humidity + "%"              + "<br><b>Weather :</b>"              + data.weather[0].description + "<br>" }  // Function call getLocation(); showPosition(); getData(); 

Explanation:

  • Using the inbuilt function, we take the Latitude and Longitude data (Refer to this)
  • Then we call an API by putting our latitude and longitude data as query parameters.
  • To know more about this API go to the https://openweathermap.org/api for official documentation.
  • After calling this API we get a JSON file we just need to fetch the data and print it.

Step 5: Now just open MainPage.html file in any browser and you will see the following output.

Reference: https://github.com/Sukarnascience/Web_Application/tree/main/Weather%20Report%20Application


Next Article
Make a Web-based Weather Report of your Location using OpenWeatherMap API

S

sukarnascience
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • HTML
  • CSS
  • Technical Scripter 2020
  • CSS-Misc
  • HTML-Misc
  • JavaScript-Misc

Similar Reads

    Create a Weather App with Forecast using React-Native
    React-Native is an open-source framework used to develop cross-platform applications i.e., you can write code in React-Native and publish it as an Android or IOS app. In this article, we will build a weather app using React-Native. The app contains two features:Getting the current weather of a given
    5 min read
    Create a weather Application using React Redux
    Weather App is a web application designed to provide real-time weather information for any location worldwide. In this article, we will make a Weather App using React and Redux. It offers a seamless and intuitive user experience, allowing users to easily access accurate weather forecasts ( temperatu
    4 min read
    Node.js Open Weather Map API for Weather Forecasts
    The Open Weather Map API is very popular as it allows you to request weather forecasts and historical weather data programmatically.Feature of Open Weather Map API:   It is easy to get started and easy to use.It is widely used and popular API for Weather Forecasts. Installation of request module:
    2 min read
    Create a Weather app using Flask | Python
    Prerequisite : Flask installation Flask is a lightweight framework written in Python. It is lightweight because it does not require particular tools or libraries and allow rapid web development. today we will create a weather app using flask as a web framework. this weather web app will provide curr
    2 min read
    Create a Weather Template App using CSS & Bootstrap
    The Weather Template App showcases a simple weather card layout with input for city names. It dynamically updates weather details such as temperature, weather conditions, humidity, wind speed, and visibility, complemented by a sun icon for sunny forecasts.PrerequisitesHTMLCSSBootstrapApproachThis We
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences