FCC:getLocation API和天气API无法生成正确的URL?

时间:2017-07-04 03:53:33

标签: jquery ajax api

我正在尝试创建一个天气应用程序,根据您的位置显示当前天气。我正在使用getLocation API和黑暗天空API - 但即使我的getLocation工作正常,我似乎也无法为暗天API获取正确的URL调用。这是我的代码,如下所示。知道是什么导致了这个吗?我认为封装makeURL()函数会解决这个问题!

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

    // Function to get latitude and longitdue and time zone
  var lat;
  var long;
  var urlAPI;
  var myKey = 'myKey';

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

  function showPosition(position) {
    lat = position.coords.latitude;
    long = position.coords.longitude;
    makeURL();
  }

  function makeURL() {
    urlAPI = 'https://api.darksky.net/forecast/'+ myKey + '/' + lat + ',' + long;
    console.log(urlAPI);
  } 

// Function to return dark sky API url with required query items
  function getCurrentWeather() {
    getLocation();
    $.ajax({
      url: urlAPI,
      dataType: 'jsonp',
      format: 'jsonp',
      data: {
        latitude: lat,
        longitude: long,
        timezone: 'America/Los_Angeles',
      },
      success: function(response) {
        //console.log(response.minutely.icon);
        console.log('hello!')
      }
    })
    console.log(urlAPI);
  }

  getCurrentWeather();

})

0 个答案:

没有答案