Using $.getJSON

Using $.getJSON
0.0 0

#1

Hello,
I’m having some trouble using the getJSON() function in order to retrieve weather information for the weather application.

    $(document).ready(function() {
      $("#getWeather").click(function() {
       if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        var url_api = "https://api.darksky.net/forecast/f8c931a74f233b551ed121edbb142777/"+lat+","+lon;
        $.getJSON(url_api, function(json) {
          $("ul").html("<li>latitude: " + json.latitude + "</li>");
          console.log()
          $("ul").html("<li>longitude: " + json.longitude + "</li>");
        });
      });
    }
      });
    });

I’m trying to approach the problem piece-by-piece by just getting my application to show me the latitude and longitude before I proceed with getting the temperature, and weather.


#2

Hi,

I am not getting into the navigator.geolocation api because I solved it another way. Which is the following and it might help you hopefully.

First get your lon and lat vars which you than paste into the url of your getJSON for darksky.

Use https://ipinfo.io/developers

    $.getJSON("https://ipinfo.io/json", function(data) {

console.log(data);
    // declare lon, lat.

    // inside this function $.getJSON for darksky since the vars need to be available.

    } );

Instead of printing something on the page as a list item just use console.log and debug in the inspector, you wanted to break it down piece by piece. Thats where console.log comes in handy. Good luck! ~Hope this helps…