A Pen by Marco Cubadda on CodePen.
Last active
November 5, 2021 18:00
-
-
Save BeatenBones/3a275d6ec92386d49c675468f23b6a87 to your computer and use it in GitHub Desktop.
Simple JQuery and JSON with OpenWeatherMap API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="city-name-wrapper"> | |
<h2 class="text-center">Weather for:</h2> | |
<h2 class="text-center" id="cityname"></h2> | |
<div id="city-forecast"> | |
<p class="weather-descr col-xs-12"></p> | |
<p class="temp col-xs-12"></p> | |
<p class="pressure col-xs-12"></p> | |
<p class="wind-spd col-xs-12"></p> | |
<div id="altit"></div> | |
<div id="latitude-val"></div> | |
<!-- forecast --> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
// geolocation enabled | |
if ("geolocation" in navigator) { | |
navigator.geolocation.getCurrentPosition(showcityname); | |
function showcityname(position) { | |
var lat = position.coords.latitude; | |
var longit = position.coords.longitude; | |
var altitude = position.coords.altitude; | |
var latitude_text = document.getElementById("latitude-val"); | |
var altitude_text = document.getElementById("altit"); | |
var city_name; | |
var temp; | |
var pressure; | |
var wind_speed; | |
var country_name; | |
var weather_description; | |
var apiKey = "5dd765a29b95b2e058dfd9f33a1dbd0d"; | |
altitude_text.innerHTML = "Altitude is " + altitude; | |
latitude_text.innerHTML = "Latitude is " + lat; | |
$.getJSON("http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + longit + "&appid=" + apiKey, function(data) { | |
city_name = data["name"]; | |
country_name = data["sys"]["country"]; | |
weather_description = data["weather"][0]["description"]; | |
temp = data["main"]["temp"]; | |
pressure = data["main"]["pressure"]; | |
wind_speed = data["wind"]["speed"]; | |
$("#cityname").html(city_name + " (" + country_name + ") " + "has " + weather_description); | |
$(".temp").html(temp); | |
$(".pressure").html(pressure + " mBar"); | |
$(".wind-spd").html(wind_speed + " m/s"); | |
}) | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h2, | |
.temp, .pressure, .wind-spd, .weather-descr, #altit, #latitude-val { | |
font-size: 40px; | |
} | |
#cityname, | |
.temp { | |
margin-top: 15px; | |
} | |
#city-name-wrapper { | |
text-align: center; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cant get it to work
regards