Show the Local Weather

This is Part 1 explaning the steps I took to complete the Show the Local Weather challenge from FreeCodeCamp‘s Intermediate Front-End Development Projects section. Part 1 consists of getting a user’s geolocation dynamically. Part 2 Part 2 will be about how to use OpenWeatherMap’s API in your app.

I used Postman to check API connectivity and to get the JSON objects. You can also simply use your browser to do the checks.

  1. First I created the site layout for my weather app using Bootstrap. This way I could play with where I wanted the person’s location to appear and how I wanted to show the weather. I used HTMLshell to quickly paste the usual tags.
  2. I created an empty CSS file and an empty JS file and included the links in my main index.html file.
  3. Then I wanted to add a button that could toggle Celsius and Fahrenheit. I needed to add JavaScript to be able to toggle the action when a user clicks the toggle button.
  4. After getting the button to work, I focused on how to get the user’s location. I used to grab a user’s IP address and use that to determine their location. It’s less accurate than Google’s Geolocation, but you can scroll to the bottom to read why that isn’t so simple to implement in Chrome since Spring this year.I knew I was using JSON so I used the url:

    When passing this url, freegeoip will automatically grab the user’s IP and return a JSON object similar to this:

    "country_name":"United States",
    "region_name":"New York",
    "city":"New York City",

    If you are using Postman, you can play around with what the response is by sending a GET to:

    //location info for
    //location info for your location
    //location info for
  5. Once you are successfully able to get the JSON object info, you can start creating your AJAX call in your JS file. I created a function called getLocation() to contain the information. I used the jQuery function $.ajax to get the data. Can also reference this. Make sure to include the jQuery link in the <head> of your html file or it won’t work!

    This is the $.ajax template:

      dataType: "json",
      url: url,
      data: data,
      success: success

    This is what my function looks like:

    var locateUser = '';
    function getLocation() {
        url : locateUser,
        dataType : "json",
        success : function(data) {
        var country =data['country_code'];
        var region = data['region_code'];
        var city = data['city'];
        var latitude = data['latitude'];
        var longitude = data['longitude'];
        $('#location').html(city + ', ' + region);
        var Weather = "" + latitude + "&lon=" + longitude + "&APPID=" + OpenWeatherKey;

    The ‘#location’ id is tied to the <div> where I want the City, Region to appear on my website.

  6. The next step is to grab the weather for the location data you just stored!

Read Part 2: Getting the Weather Information here.

Final Product:

Note: When attempting to complete this challenge, do not use Google’s Geolocation as since May 2016, they require do not work over insecure domains (must use HTTPS). Free SSL here.

If you insist on trying to implement Google’s Geolocation, you can sign up for an API key here: Then follow Udacity’s really great tutorial on using it:–ud864.

Keep in mind that Chrome and Safari (webkit browsers) do not allow Geolocation API for files that are local. Geolocation WILL work in Internet Explorer as of this posting.

Another weather API you can use:
Another IP geolocation API:

See for details.



One thought on “Show the Local Weather

  1. Pingback: Show the Local Weather – Part 2 | C'est mon voyage

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s