JavaScript issue with global variable assignment - javascript

I'm trying to create a weather widget using below code snippets. However, the global variables longitude and latitude values are not getting updated in the success function. I have tried all the combinations of globalThis and window objects but yet not been able to resolve the issue.
setInterval(showWeather, 900000);
setTimeout(showWeather,1000)
function showWeather(){
var appid = "API_KEY_FOR_OPENWEATHERMAP";
var latitude = 0;
var longitude = 0;
function success(position){
window.latitude = position.coords.latitude;
window.longitude = position.coords.longitude;
}
function error(){
console.log('Some error occurred while retrieving your device location! Hence showing the default location weather!')
}
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(success, error);
}else{
console.log("Your device doesn't support geolcation tracking! Hence showing the default location weather!")
}
async function fetchWeather(){
var url = `https://api.openweathermap.org/data/2.5/weather?lat=${window.latitude}&lon=${window.longitude}&appid=${appid}&units=metric`
const response = await fetch(url);
data = response.json();
return data
}
fetchWeather().then(response=>{
const icon = response.weather[0].icon;
document.getElementById("city").innerHTML = response.name;
document.getElementById("temp").innerHTML = response.main.temp + "°";
url = `https://openweathermap.org/img/w/${icon}.png`;
document.getElementById("wicon").src = url;
})
}
<h5 id="city">User Location</h5>
<div>
<img id="wicon" src="https://openweathermap.org/img/w/01d.png" alt="Weather icon">
<strong id="temp">Temperature°</strong>
</div>

getCurrentPosition() is asynchronous, but you're not waiting for it to finish before using the results. You should call fetchWeather() from the success() function, since that's called when getCurrentPosition() finishes.
There's no need to use global variables for latitude and longitude. Pass them as arguments to fetchWeather().
setInterval(showWeather, 900000);
setTimeout(showWeather, 1000)
function showWeather() {
var appid = "API_KEY_FOR_OPENWEATHERMAP";
function success(position) {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
fetchWeather(latitude, longitude).then(response => {
const icon = response.weather[0].icon;
document.getElementById("city").innerHTML = response.name;
document.getElementById("temp").innerHTML = response.main.temp + "°";
url = `https://openweathermap.org/img/w/${icon}.png`;
document.getElementById("wicon").src = url;
})
}
function error() {
console.log('Some error occurred while retrieving your device location! Hence showing the default location weather!')
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
console.log("Your device doesn't support geolcation tracking! Hence showing the default location weather!")
}
async function fetchWeather(latitude, longitude) {
var url = `https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=${appid}&units=metric`
const response = await fetch(url);
data = response.json();
return data
}
}
<h5 id="city">User Location</h5>
<div>
<img id="wicon" src="https://openweathermap.org/img/w/01d.png" alt="Weather icon">
<strong id="temp">Temperature°</strong>
</div>

Related

How to get an API URL to take in a dynamic parameter to allow user input?

I am trying to make city a dynamic parameter that will change in my weather app based on which city the user inputs.
I want to pass city into the getWeatherInfo function. Depending on where I try to assign city, I keep getting back errors such as:
city is undefined,
city not found, or
city cannot be accessed before it is initialized.
I cannot see how to make it work. This is my code:
const apiTagUrl = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + city + ",USA&units=imperial&appid=" + apiTag);
async function getWeatherInfo() {
const response = await fetch(apiTagUrl);
const data = await response.json();
console.log(data);
};
getWeatherInfo();
You must put city, like argument to getWeatherInfo function and pass it to string at new URL. Check this code:
const apiTag = "xxxxxx"
async function getWeatherInfo (city) {
const apiTagUrl = new URL ("https://api.openweathermap.org/data/2.5/weather?q=" + city +",USA&units=imperial&appid=" + apiTag);
const response = await fetch (apiTagUrl);
const data = await response.json();
console.log(data);
};
function find(){
const userValue = document.getElementById("cityInput")
getWeatherInfo(userValue);
}
<input placeholder="City..." id="cityInput" />
<button onclick="find()">Get weather</button>

geolocation keeps on asking for permission

I was testing geolocation API and found if I refresh my page, the page keeps on asking for permission, so I saved my coordinate data to local storage but it doesn't works! Is there any way to give permission only once???
const COORDINATION = "coords";
function saveCords(coordsOBJ){
localStorage.setItem(COORDINATION,JSON.stringify(coordsOBJ));
}
function handleGeoError(position){
console.log("Cant find position");
}
function handleGeoSuccess(position){
// console.log(position);
const latitude = position.coords.latitude;
console.log(latitude);
const longitude = position.coords.longitude;
const coordsOBJ = {
latitude,//latitude = latitude,
longitude//longitude = longitude
}
saveCords(coordsOBJ);
}
function askForCoords(){
navigator.geolocation.getCurrentPosition(handleGeoSuccess,handleGeoError);
}
function loadCoordinate(){
const loadedCords = localStorage.getItem("COORDINATION");
if(loadedCords === null)
{
askForCoords();
}
}
function init(){
loadCoordinate();
}
It looks like there is a typo in your code, whereby you've added quotes to COORDINATION but it's a varible not a string.
Try changing:
const loadedCords = localStorage.getItem("COORDINATION");
To:
const loadedCords = localStorage.getItem(COORDINATION);

"weather is not defined" error when working with openwethermap API

I'm somewhat new to working with API's using vanilla JavaScript and I keep running into this error when trying to access the "description" within the "weather object". The console keeps reading "weather is not defined". I'm using the open weather map API. In theory I should be able to retrieve using data.current.weather.description. but that doesn't work, along with the other variations I've tried. Here is my current code.
window.addEventListener("load", () => {
let long;
let lat;
let temperatureDescription = document.querySelector(
".temperature-description"
);
let temperatureDegree = document.querySelector(".temperature-degree");
let locationTimezone = document.querySelector(".location-timezone");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
long = position.coords.longitude;
lat = position.coords.latitude;
const proxy = "https://cors-anywhere.herokuapp.com/";
const api = `${proxy}https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${long}&
exclude=hourly,daily&appid=da5463709c92ab1860d3a81037565c6e`;
fetch(api)
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
const {
temp,
weather: { description },
} = data.current;
//Set DOM Elements from the API
let kelvinToCelsius = temp - 273.15;
temperatureDegree.textContent = kelvinToCelsius.toFixed(0);
temperatureDescription.textContent = description;
locationTimezone.textContent = data.timezone;
});
});
} else {
}
});
If anyone has ran into this issue and solved, it would be much appreciated if you filled me in.

How to make jQuery constructor properties globally visible

I am trying to get position coordinate variables using the standard Navigator.geolocation property with jquery, so i can use the value later in my code:
$(document).ready(function(){
$.getlocation = function(){
navigator.geolocation.getCurrentPosition($.getPosition,$.error);
}
$.getVariables = function(lat,lon){
this.lat = lat; // i want these to be visible
this.lon = lon;
}
$.getPosition= function(position){
console.log("latitude:" +position.coords.latitude+ " longitude: "+position.coords.longitude);
//this function will be executed once position is determined.
$.getVariables(position.coords.latitude,position.coords.longitude);
}
$.error = function(){alert("error");}
$.getlocation(); // outputs correctly
setTimeout(()=>{console.log(this.lat)},5000); // undefined
});
I expect to get location output but instead i get undefined from console.log(this.lat), i did try this in vanilla javascript and it works fine, here is the javascript code:
function locateMe() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getPosition, error);
} else {
alert("connection problem");
}
}
let vars = function(lat, lon) {
this.lat = lat;
this.lon = lon;
}
let getPosition = function(position) {
vars(position.coords.latitude, position.coords.loongitude);
}
let error = function(msg) {
console.log("problem");
}
locateMe();
setTimeout(() => { console.log(this.lat); }, 5000); //correct output
I could get this.lat working if I changed getVariables to:
$.getVariables = function(lat,lon){
document.lat = lat;
document.lon = lon;
}
It appears that the two this objects may refer to different things in the two methods.

Why can't $(document).ready() be inside API call?

FYI Trial 1 does not work, but Trial 2 works.
I understand that getJSON is executes asynchronously, but I don't actually understand how it applies to the code I've written.
What can I learn about asynchronous execution from this?
Why do I have to separate the getJSON call into a function separate from ready()?
FOR TRIAL 2:
How can I write this code so I don't have to initialize functions inside of getJSON? If there isn't a way, how can I write this code to be more robust?
/*
//TRIAL 1
$(document).ready(function(){
console.log("loaded");
$.getJSON(url, function(json){
var fahrenheit = true;
getLocation(json);
getTemperature(fahrenheit, json);
$("#unit").on("click", function(){
fahrenheit = !fahrenheit;
getTemperature(fahrenheit, json);
});
getWeather(json);
});
});
//Gets current weather conditions from current_observation
function getWeather(json){
var currWeather = "";
var iconURL = "";
currWeather=json.current_observation.weather;
iconURL=json.current_observation.icon_url;
$("#icon").attr("src", iconURL);
$("#weather").html(currWeather);
};
//Gets current temperature from current_observation
function getTemperature(fahrenheit, json){
var currTemp = 0;
if(fahrenheit){
currTemp+=json.current_observation.temp_f;
currTemp+="&#8457";
} else{
currTemp+=json.current_observation.temp_c;
currTemp+="&#8451";
}
$("#temperature").html(currTemp);
};
//Gets city, state, country, zip, latitude, and longitude from location
function getLocation(json){
var currLocation=["city", "state", "country", "zip", "lat", "lon"];
var locationHTML = "";
currLocation[0] = json.location.city;
currLocation[1] = json.location.state;
currLocation[2] = json.location.country_name;
currLocation[3] = json.location.zip;
currLocation[4] = json.location.lat;
currLocation[5] = json.location.lon;
locationHTML += currLocation[0]+", "+currLocation[1]+", "+currLocation[2]+" " +currLocation[3]+"<br>";
locationHTML += "Latitude: "+currLocation[4]+"<br>Longitude: "+currLocation[5];
$("#location").html(locationHTML);
};
*/
//TRIAL 2
$(document).ready(function(){
console.log("loaded");
dispWeather();
});
function dispWeather(){
console.log("inside dispWeather");
//Retrieve json from weather underground
var url = "https://api.wunderground.com/api/19c5c96f0b140c0f/geolookup/conditions/q/autoip.json";
$.getJSON(url, function(json){
console.log("Got JSON");
console.log(json);
var fahrenheit = true;
getLocation(json);
getTemperature(fahrenheit, json);
$("#unit").on("click", function(){
fahrenheit = !fahrenheit;
getTemperature(fahrenheit, json);
});
getWeather(json);
//Gets current weather conditions from current_observation
function getWeather(json){
var currWeather = "";
var iconURL = "";
currWeather=json.current_observation.weather;
iconURL=json.current_observation.icon_url;
$("#icon").attr("src", iconURL);
$("#weather").html(currWeather);
};
//Gets current temperature from current_observation
function getTemperature(fahrenheit, json){
var currTemp = 0;
if(fahrenheit){
currTemp+=json.current_observation.temp_f;
currTemp+="&#8457";
} else{
currTemp+=json.current_observation.temp_c;
currTemp+="&#8451";
}
$("#temperature").html(currTemp);
};
//Gets city, state, country, zip, latitude, and longitude from location
function getLocation(json){
var currLocation=["city", "state", "country", "zip", "lat", "lon"];
var locationHTML = "";
currLocation[0] = json.location.city;
currLocation[1] = json.location.state;
currLocation[2] = json.location.country_name;
currLocation[3] = json.location.zip;
currLocation[4] = json.location.lat;
currLocation[5] = json.location.lon;
locationHTML += currLocation[0]+", "+currLocation[1]+", "+currLocation[2]+" " +currLocation[3]+"<br>";
locationHTML += "Latitude: "+currLocation[4]+"<br>Longitude: "+currLocation[5];
$("#location").html(locationHTML);
};
})
};
.ready() jQuery Documentation
Specify a function to execute when the DOM is fully loaded.
What can I learn about asynchronous execution from this?
Your learning that you don't know when the document is going to be ready() so we wait until the event completes before beginning execution on our application. You also learned that you have to wait for $.getJSON to fetch json then you process the data.
Why do I have to separate the getJSON call into a function separate from ready()?
As specified above .ready() is waiting for the DOM to be fully loaded, then we start the application. So when we are "ready" lets fetch the weather data. The document is only ready one time when the DOM is fully loaded.
How can I write this code so I don't have to initialize functions inside of getJSON?
Without you being specific, I'm assuming your problem here was with toggling the degrees between celsius and fahrenheit. After we load the weather you can store the data in a variable outside of the scope of the function, this way when you click to change the degrees you can pass in the same data without having to call the api again (although at this point the weather could have changed)
how can I write this code to be more robust?
I've included a JS Bin that alters your code. The biggest problem was bad naming conventions and not keeping things simple. Example getWeather() was not "getting weather" it was setting html from data we got from $.getJSON which was invoked in your ready() instead of breaking it out into another function we could call later on.
For the most part this is how the code reads now, clear function names help quickly see what this code is supposed to do.
$(document).ready(function() {
renderWeather();
});
var state = {
fahrenheit: true,
data: {}
};
function renderWeather() {
getWeatherJSON()
.then(data => {
state.data = data;
setWeatherHTML(data);
setTemperatureHTML(data, state.fahrenheit);
setLocationHTML(data);
})
.catch(err => console.log(err));
}
http://jsbin.com/cekite/edit?js,output
If we wanted to take this a step further we could create a WeatherAPI prototype concealing our html render functions and extend it with a WeatherUndergroudAPI prototype, this way if we ever change our weather service we should only have to implement a format function to marshall the data the way the WeatherAPI expects it to be in.
class WeatherAPI {
constructor(opt) {
...
}
init() {
...
}
get() {
... feteches json from endpoint provided
}
renderWeather() {
this.get()
.then(this.formatter.bind(this))
.then(this.setWeatherData.bind(this))
.then(this.renderWeatherHTML.bind(this))
.then(this.renderTemperatureHTML.bind(this))
.then(this.renderLocationHTML.bind(this))
.catch(err => console.log(err));
}
formatter(data) {
...
}
setWeatherData(data) {
...
}
renderWeatherHTML() {
...
}
renderTemperatureHTML() {
...
}
renderLocationHTML() {
...
}
}
Extending the WeatherAPI is then a matter of passing in a new endpoint to get data from. Or in this case overriding the WeatherAPI get method and returning static data.
class FakeWeatherAPI extends WeatherAPI {
constructor(opt = {}) {
super(opt);
}
get() {
return new Promise((resolve) => {
const data = {
someReallyWeirdKeyForACity: 'San Francisco',
state: 'CA',
country: 'US',
lat: '37.77999878',
lon: '122.41999817',
f: '1000',
c: '25',
icon: 'http://www.nyan.cat/cats/zombie.gif',
weather: 'Acid Rain'
};
resolve(data);
});
}
formatter(data) {
const formattedData = {
city: data.someReallyWeirdKeyForACity,
state: data.state,
country: data.country,
lat: data.lat,
lon: data.lon,
temperature: {
fahrenheit: data.f,
celsius: data.c
},
icon: data.icon,
weather: data.weather
};
return formattedData;
}
}
Our application init code then becomes.
$(document).ready(init);
function init(){
const weatherAwesomeService = new FakeWeatherAPI();
weatherAwesomeService.init();
};
Here is a working jsbin for they above
http://jsbin.com/sicofe/edit?js,output

Categories

Resources