udacity / ud811

Course materials for Udacity's Intro to Progressive Web Apps course at https://www.udacity.com/course/intro-to-progressive-web-apps--ud811
MIT License
177 stars 437 forks source link

Weather API access solution #5

Open akg314 opened 6 years ago

akg314 commented 6 years ago

If you're having trouble setting up a proxy server because the firebase API doesn't work, here's another solution (you need chrome to make this work).

  1. Register on https://darksky.net/dev and get an API key
  2. Download the https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en extension to allow CORS requests (only turn it on for your project server - turn it off for other sites or it might cause problems loading content!)
  3. Edit the app.getForecast function in your app.js file as follows, inserting your API key from step 2:
    
    app.getForecast = function(key, label) {
    var cityToLatLon = {
      'austin': '30.3079823,-97.8938293',
      'boston': '42.313479,-71.1273685',
      'chicago': '41.8339032,-87.8725822',
      'bangalore': '12.9539598,77.4905096',
      'buenosaires': '-34.6158036,-58.5035321',
      'capetown': '-33.9137107,18.0942365',
      'london': '51.5285578,-0.2420244',
      'mcmurdo': '-77.8401137,166.6441437',
      'newyork': '40.7128,-74.0059',
      'portland': '45.5425909,-122.7948484',
      'sanfrancisco': '37.7578149,-122.5078116',
      'seattle': '47.6149938,-122.4763319',
      'sydney': '-33.7966053,150.6415579',
      'tokyo': '35.6732615,139.5699601'
    };
    var cityCoords = cityToLatLon[key];
    var url = 'https://api.forecast.io/forecast/INSERT_YOUR_API_KEY_HERE/' + cityCoords;
    // Make the XHR to get the data, then update the card
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
      if (request.readyState === XMLHttpRequest.DONE) {
        if (request.status === 200) {
          var response = JSON.parse(request.response);
          response.key = key;
          response.label = label;
          app.updateForecastCard(response);
        }
      }
    };
    request.open('GET', url);
    request.send();
    };