Tell us what’s happening:
Weirdest thing - so I have a working local weather app. Wanted to build it with Web Components, simply to see if I could, and it works quite nicely. But the strangest thing is that the API returns a different JSON structure for the same URL (the lat & lon remain the same, but the returned JSON object is different). Also, there is no icon in the returned json’s weather[0].icon
.
I’m baffled. Anybody got an idea?
Your code so far
The relevant section…
const weatherAPIUrl = "https://fcc-weather-api.glitch.me/api/current?";
// Convenience functions
// Take the location object and pull out its latitude and longitude, and return a URL
const updateWeatherApi = ({lat, long})=>`${weatherAPIUrl}lon=${long}&lat=${lat}`;
// We'll get temps in C. Convenience function to convert them to F.
const tempInF = (temp) => (temp*9/5+32).toFixed(1);
// The workhorse. This gets the current location, then calls the API to get the weather.
const getWeather = ()=>{
// Call to get the current location.
navigator.geolocation.getCurrentPosition( (location) => {
// Convert our complex object to a simpler one, just for convenience sake.
const locationObj = {lat: location.coords.latitude.toFixed(2), long: location.coords.longitude.toFixed(2)};
const tempUrl = updateWeatherApi(locationObj);
console.log(tempUrl);
fetch(tempUrl)
.then(response=>response.json())
.then(jsonObj=>{
console.log(jsonObj);
weatherObject = JSON.parse(JSON.stringify(jsonObj));
updateWeatherDisplay();
});
}, error=>{console.error(JSON.stringify(error)); })
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
.
Challenge: Show the Local Weather
Link to the challenge: