Weather API and taking the information


#1

I feel like I am missing something drastic here. I am not totally getting how to get the temp. I can get the location, but I don’t know what to do from there. Any help I would appreciate.

//
$(document).ready(function(){

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
$("#city").html(“latitude:” + position.coords.latitude + “
longitude:” + position.coords.longitude);

});

}

var long = position.coords.longitude;
var lat = position.coords.latitude;
var weatherSite = “https://fcc-weather-api.glitch.me/api/current?lon=” +long +"&lat=" + lat;

$.getJSON(weatherSite).done(function(data){
$("#Temp").html(“Tempture is” + data.main.temp);

});

});

//


#2

You’re doing pretty much fine here, but I think you have trouble understanding asynchronous programming with callbacks. I did format your code, you can see it below:


//
$(document).ready(function() {

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            $("#city").html(“latitude: ”+position.coords.latitude + “
                longitude: ”+position.coords.longitude);

        });
    }

    var long = position.coords.longitude;
    var lat = position.coords.latitude;
    var weatherSite = “https: //fcc-weather-api.glitch.me/api/current?lon=” +long +"&lat=" + lat;

        $.getJSON(weatherSite).done(function(data) {
            $("#Temp").html(“Tempture is” + data.main.temp);
           
        });

});

//

Now, let’s have a look. You call:

 navigator.geolocation.getCurrentPosition(function(position) {
       $("#city").html(“latitude: ”+position.coords.latitude + “
            longitude: ”+position.coords.longitude);
       // we are now in scope of callback function
       // here is you should do all operations involving position object
});

and inside your callback function (executed asynchronously) you get position object containing your latitude and longitude. You even use it in your HTML. So, basically, if you want to use position object, you must do it within this callback function, because it’s the only place (scope) where position object is accessible.

Please give it a shot and if you still can’t get it done, I will give you a small hint, deal? :wink:


#3

Thank you for the help. From what I understand, all i need to do is move the function up to that section…

\
$(document).ready(function(){

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
$("#city").html("latitude: " + position.coords.latitude + "
longitude: " + position.coords.longitude);

var long = position.coords.longitude;
var lat = position.coords.latitude;
var weatherSite = “https://fcc-weather-api.glitch.me/api/current?lon=”+long +"&lat="+lat;

$.getJSON(weatherSite).done(function(data){
$("#Temp").html(“Tempture is” + data.main.temp);

});
});

}

});

\\