0

I want the longitude and latitude passed from the geolocation function to my getjson function so that i can pull certain info like temperature, condition etc. What am i doing wrong that the lati and loni are not being passed properly? Are my functions set up correctly?

$(window).load(function() {

  getLocation();
  apiCall();

}); //run immediately on page load


var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";
  }
}

function showPosition(position) {
  var lati = position.coords.latitude;
  var loni = position.coords.longitude;

  function passData(lati, loni);
}



var weath = 0;

function apiCall() {
  function passData(latitude, longitude) {
    var lati = latitude;
    var loni = longitude;
    x.innerHTML = "Latitude: " + lati +
      "<br>Longitude: " + loni;
  }
  //$(".city").html(data.city + "," + "" + data.countryCode);


  $.getJSON("https://fcc-weather-api.glitch.me/api/current?lat=" + lati + "&lon=" + loni, function(data1) {

    weath = Math.round(data1.main.temp);

    $(".temp").html(weath + " " + "&#8451");
    $(".condition").html(data1.weather[0].description);

    $(".iconDisplay").append('<img src=' + data1.weather[0].icon + '/>');
  }); /*end of json*/

};
user2763557
  • 449
  • 5
  • 18
  • This is wrong.. `function passData(lati, loni);`. What are you trying to achieve there? – choz Mar 18 '18 at 15:48
  • i want to push the latitude and longitude data to my json. How would i do that? – user2763557 Mar 18 '18 at 15:49
  • There's couple of things you need to pay attention of. Things like [Function scoping](http://ryanmorr.com/understanding-scope-and-context-in-javascript/) for your `passData` which needs to be called outside of `apiCall` function. And [chaining an asynchronous function](https://stackoverflow.com/questions/39028882/chaining-async-method-calls-javascript) which you also need for calling `apiCall` after `getLocation` is done. – choz Mar 18 '18 at 16:07

0 Answers0