Values Not Reflecting Weather In My Area?

Values Not Reflecting Weather In My Area?
0.0 0

#1

This is my code:

I’m curious about how the API works. I manually set the lat and long for my location (toronto) and it says the temperature is 23 but without any units. It’s definately not 23 celcius or fahrenheit here so I’m a little bit confused as to what the lat and long input actually does. Or how I change it to display values for my area. Thanks!


#2

API works like this --> var api = "https://fcc-weather-api.glitch.me/api/current?lat=" + lat + "&lon=" + lon;

How do you get lon and lat?


#3

How come my latitude and longitude isn’t changing though? It still shows Lat:35, Long: 139…Also the temperature is not reflecting the temperature at my location (toronto). I find some times when i refresh, it shows 35 and 139 and other times it shows 43 -79


#4

What browser are you using?


#5

@John-freeCodeCamp i am using google chrome for this


#6

Same here. I have no idea why its not working.

Try running this code in your at the top of your js section in codepen and check the console -->

$(document ).ready(function() {
  navigator.geolocation.getCurrentPosition(function(position) {
    console.log( position.coords.latitude, position.coords.longitude)
     });
  });

I did it, and got the correct lon and lat.

Make sure $(document ).ready(function() { }); covers everything.

Here is what i did for my project

$(document).ready(function() {
  var lon;
  var lat;
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      lon = position.coords.longitude;
      lat = position.coords.latitude;
      var api =
        "https://fcc-weather-api.glitch.me/api/current?lat=" +
        lat +
        "&lon=" +
        lon;
      $.getJSON(api, function(data) {
             // code
            });
        });
     }
});

If this doesn’t work, create another forum post asking why lat and lon is wrong.


#7

Alright it seems to work in firefox, does that narrow the problem down at all?


#8

Does the code i listed above work on chrome. The one i did for my project?

Give all the code you test at least 20 seconds.


#9

just updated it, still doesn’t seem to work :confused: It’s weird how it only seems to work some times. Like maybe 1/50 refreshes but on fox it seems to work more frequently. Still not 100% of the time though…


#10

Create a separate forum post on why your not getting the right lon and lat.

@randelldawson might be able to help


#11

Take a look at the following open ticket regarding the FCC weather api. Your code correctly gets the user’s latitude and longitude, but because of the issue related to the weather api, you are getting the default weather for Shuzenji, Japan.