Hi everyone,
The local weather project is driving me crazy. My javascript code is below, and I am not sure what I am doing wrong. Nothing happens at all when I click my button (#getWeather). But, if I comment out the bottom half of the code, I can get the lat and lon coordinates to appear in my div (#displayBox). Similarly, if I comment out the geolocation part of the code and hard code the coordinates into the openweather api url, then I can get the weather information to display in the div. But when I try to do all of it, something is not working.
I have a feeling it is something trivial I am overlooking, and I canât figure it out. If anyone has suggestions, I would love to hear them.
Thanks
$(document).ready(function() {
$("#getWeather").on(âclickâ, function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var x = position.coords.latitude;
var y = position.coords.longitude;
$.getJSON(âhttp://api.openweathermap.org/data/2.5/weather?lat="+x+"&lon="+y+"&units=imperial&APPID=e9b433f7ed306860db69ea25723a5f48â, function(data) {
var city = data.name;
var temp = Math.round(data.main.temp);
var weather = data.weather[0].description;
var image = ââ;
$("#displayBox").html(city+"
"+temp+"
"+weather+" â+image);
});
});
} else {
$(â#displayBox").html(âUnable to access geolocation data.â);
}
});
});
What does the line with the $("#displayBox").html()
say exactly? Because it works fine for me without using whatever text you put in between. Also whatâs in the image variable?
BTW: Your code will be displayed better, if you select your code and press </>
.
Thanks for your reply, and thanks also for the heads up about how to post code. I was wondering how people did that.
On a whim, I decided to switch browsers. Voila! On Firefox my code works perfectly. On Chrome: no so much. I wonder what the problem is?
And here is my code in better form hopefully:
$(document).ready(function() {
$("#getWeather").on("click", function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var x = position.coords.latitude;
var y = position.coords.longitude;
$.getJSON("http://api.openweathermap.org/data/2.5/weather?lat="+x+"&lon="+y+"&units=imperial&APPID=e9b433f7ed306860db69ea25723a5f48", function(data) {
var city = data.name;
var temp = Math.round(data.main.temp);
var weather = data.weather[0].description;
var image = "<img src='http://openweathermap.org/img/w/" + data.weather[0].icon + ".png'>";
$("#displayBox").html(city+"<br><br>"+temp+"<br><br>"+weather+" "+image);
});
});
} else {
$("#displayBox").html("Unable to access geolocation data.");
}
});
});
I really wish FCC did a better job explaining this challenge. took me two days to figure out (it was before we had the forums), and I have seen lot of other people here with the same issue.
The problem is, you canât use navigator.geolocation and the openweather map at the same time. Why? navigator.geolocation requires that the page be loaded in a secure connection (https://). OpenWeatherMap free version does not let you access data if your page is loaded with a secured connection. You will get something like âMixed content: the page at ⌠requested data from http://⌠but was loaded over httpsâ something like that in the console. The point is, if you want to use OpenWeatherMap, you cannot use navigator.geolocation. I would recommend using the IP-API - its super simple. Send a request to http://ip-api.com/json like you did with the weather api. You can get everything you need about the userâs position such as lat, lon, etc. from the ip-api object.
1 Like
Chrome doesnât support geolocation on insecure sites (http). So you would have to use https, but then your api wonât work, because Chrome will also not let you make http api request on https website. You can work around this by using $.getJSON("https://crossorigin.me/http://api.openweathermap.org/.......
and then opening codepen (if you are using that?) with https.
that is an interesting link, just thought I should add, that codepen is not https by default so if someone looks at your portfolio on codepen this will not work.
Thatâs true. Though most people will only see this after you link to it. But you could add a comment at the top of the HTML/CSS/JS for those people. @BobWinn please note that site (http://crossorigin.me) is quite often down, so the solution of @IsaacAbrahamson might be better
That is great to know. Thanks a lot. I think I will switch over to the ip-api instead.
Thanks for the reply. Good to know about the downside to the IP API, and I too want this to be a versatile app. I will definitely look into the Dark Sky API.
One thing about the Dark Sky API (everything has a caveat ;))âthe location things like the city, state, country, etc. arenât included in the JSON object, so if you want those, youâll have to figure out how to get them. I used Googleâs geocoding API and input the latitude and longitude to get location info. Just a heads up! I have seen people who have just printed the latitude and longitude on the weather app instead of city/state/country.
1 Like
How did you do that? Currently trying to find out how to get that going. Also, did you use Skycons? I am having a difficult time trying to get all this to work correctly. My CodePen is here: https://codepen.io/twhite96/pen/OXEEJG/
Hereâs the docs for Googleâs Reverse Geocoding API. Try it out for yourself, and feel free to ask any questions once you have.
I did use Skycons. They seem to be showing up fine for meâwhatâs the issue youâre having with them?
Iâve looked at the API docs but it says I need to display a map? At least thatâs what I got out of it.
I am still searching for examples of using the API. Thanks for the tip.
Look at this link: https://developers.google.com/maps/documentation/geocoding/start
Scroll down almost to the bottom, and search âReverse geocoding request and response (address lookup)â.
Youâll find an example there, with longitude and latitude in the URL. (You wonât actually need an API key, it works without it just fine. )
I hope this helps to solve your problem.
Good luck with it!
1 Like
It looks like you got it to work! Congratulations! Iâm working on mine right nowâŚ
@gsim2345 Thanks! I looked at the website, and it seems very helpful. Although, how did you input the (https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY)? I just put it in an < a > tag with href, and it wonât work. Any suggestions?