Not getting data from forecast.io

Not getting data from forecast.io
0

#1
function getLocation(position) {
  var lt = position.coords.latitude;
  var lg = position.coords.longitude;


$("document").ready(function(lt, lg){
  var apiKey = "2e591f122507ef51a781deaff8643744";
  var durl = "https://api.forecast.io/forecast/";
  var lati = lt;
  var longi = lg;
  var weatherURL = durl + apiKey + "/" + lati + "," + longi + + "?callback=?";
  var data;

Here is the code. I’m not getting the data back from forecast.io
Can someone check what the error is?


#2

$("document").ready(function(){}); doesn’t take parameters. Also you don’t seem to make any AJAX requests?


#3

Then how do I pass latitude (lt) and longitude(lg) to lati and longi variables?


#4

Your syntax is not setup right

  1. You have no closing bracket for your function getLocation.
  2. you are not making any request in your getLocation function
  3. You don’t put $(“document”).ready inside another function if that’s what you are doing.
  4. You use $(document).ready() not $(“document”).ready()
  5. There really are a lot of other syntax errors, but here is a start, figure it out from here:
$("document").ready(function(){
  var lt = position.coords.latitude;
  var lg = position.coords.longitude;
  var apiKey = "2e591f122507ef51a781deaff8643744";
  var durl = "https://api.forecast.io/forecast/";
  var weatherURL = durl + apiKey + "/" + lati + "," + longi + + "?callback=?";
  var data;

  $.getJson(weatherURL, json) {
    alert(json.data);
  }
});
  

This will not work off the bat, but I would suggest going over the jquery and JSON api sections again, and that will help you.


#5

You would pass the variables from the $(document).ready() into the function getLocation(lat, lon);

You did something like this:

function getLocation(position) {
  var lt = position.coords.latitude;
  var lg = position.coords.longitude;


$("document").ready(function(lt, lg){
  var apiKey = "2e591f122507ef51a781deaff8643744";
  var durl = "https://api.forecast.io/forecast/";
  var lati = lt;
  var longi = lg;
  var weatherURL = durl + apiKey + "/" + lati + "," + longi + + "?callback=?";
  var data;

Forgetting the syntax errors, your function getLocation is never invoked. It never runs. Like BenGitter said, you don’t pass arguments to the ready() function. If you want to use parameters and a second functino do it like this:

$("document").ready(function(){
  //get geolocation here
  var lt = geolocation.position.coords.latitude;
  var lg = geolocation.position.coords.longitude;
  getData(lt, lg);
});

function getData(lati, lonig) {
  var apiKey = "2e591f122507ef51a781deaff8643744";
  var durl = "https://api.forecast.io/forecast/";
  var weatherURL = durl + apiKey + "/" + lati + "," + longi + + "?callback=?";
  var data;

  $.getJson....
}

#6

Thanks @IsaacAbrahamson. I’ll look into it.