skip to Main Content

I am building a weather app that shows the weather condition of each city using a weather IPA.
I am trying to display the wind direction in degrees but I could only display in a textual description ex: Northerly’, ‘North Easterly’, ‘Easterly..
I want to display the wind in degrees as well.

How to show the wind direction in a degree (e.g 90°) in javascript (including the use of the small circle degree symbol after the number)?

Because I am working with data files I can’t make the code work outside of the text editor if you would like to see the full code.

I could only share a part of the code which is the main thing. But don’t worry, the part I couldn’t share are the data files of the cities — please select — for each country —select a country. —

In this code, I am displaying the wind direction as a textual description, how to display in degrees as well?

function text(d) {
        let directions = ['Northerly', 'North Easterly', 'Easterly', 'South Easterly', 'Southerly', 'South Westerly', 'Westerly', 'North Westerly'];

        d += 22.5;

        if (d < 0)
            d = 360 - Math.abs(d) % 360;
        else
            d = d % 360;

        let w = parseInt(d / 45);
        return `${directions[w]}`;
    }
function getData(selectedCounty) {
     $.ajax({
         url: 'http://api.openweathermap.org/data/2.5/weather?id=' + selectedCounty + '&appid=e4761ea183f1b15b7c6af8e63724a863',
         type: 'GET',
         dataType: 'json',
         success: function(response) {
         
             //display the chosen city and date from the requested data
             $('#cityName').empty().append('Weather for ' + response.name + ' on ' + toDD_MM_YY_format(response.dt));
             //display the city name div after the user choose country and city

             $('#weatherInfo').empty().append(
                 'Weather Conditions: ' + response.weather[0].main + '</br>' +
                 'Temperature :' + toCelsius(response.main.temp) + '</br>' +
                 'Wind Speed :' + toMilesPerHour(response.wind.speed) + '</br>' +
                 'Wind Direction: ' + text(response.wind.deg));
             displayWeatherIcon(response.weather[0].icon);
         },
         error: function() {
             $('#errorInfo').show().html('<p> An error has occurred, Please try again later</p>');
             $('#weatherInfo').empty();
         }
     });
 }

 //convert temperature in kelvin to Celsius.
 function toCelsius(kelvin) {
     var tempInCelsius = Math.round(kelvin - 273.15);
     return tempInCelsius + '°C';
 }

 //converts speed in knots to miles per hour(mph)
 function toMilesPerHour(knots) {
     var speedInMilesPerHour = Math.round(knots * 1.15077945); //1 Knot = 1.15077945 mph
     return speedInMilesPerHour + ' mph';
 }

 //converts UNIX time stamp in to readable format
 function toDD_MM_YY_format(unixTimeStamp) {
     var d = new Date(unixTimeStamp * 1000);
     var month = (d.getMonth()) + 1; //unix time stamp month starts from 0.
     var formattedDate = d.getDate() + "-" + month + "-" + d.getFullYear();
     return formattedDate;
 }
//convert the wind direction from degree to textual description.
function text(d) {
        let directions = ['Northerly', 'North Easterly', 'Easterly', 'South Easterly', 'Southerly', 'South Westerly', 'Westerly', 'North Westerly'];

        d += 22.5;

        if (d < 0)
            d = 360 - Math.abs(d) % 360;
        else
            d = d % 360;

        let w = parseInt(d / 45);
        return `${directions[w]}`;
    }
<!DOCTYPE html>
<html>

<head>
    <title>UK Weather Application</title>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="apiweather.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
    <header>
        <h1>UK Weather Data</h1>
    </header>
    <section id="content">
        <h4>Current Weather Data</h4>
        <select id="countries">
            <option selected>Select a Country</option>
            <option value="New York">New York</option>
            <option value="Argentina">Argentina</option>
            <option value="Australia">Australia</option>
            <option value="Portugal">Portugal</option>
        </select>
        <select id="counties">
            <option selected>&lt;&lt;Please Select</option>
        </select>
       
        
        <div id="cityName"></div>
        <div id="weatherInfo"></div>
        <div id="errorInfo" hidden></div>
    </section>
    <footer>
    </footer>
</body>

</html>

Hope I explained well, please don’t hesitate to ask me questions.

thank you.

2

Answers


  1. because you are using weather API im not really sure but maybe you could try using something like this in your text() function

    const windDegree = String.fromCharCode(0xfeff00b0);
    deg = 90;
    
    console.log(`${deg}${windDegree} = ${text(deg)}`);
    Login or Signup to reply.
  2. Try something like this

    let WIND_DIRECTION;
        function getWindDirection(d){
            switch (true) {
                case 0 :
                case 360:
                    WIND_DIRECTION = "N";
                break;
                case 90 :
                    WIND_DIRECTION = "E";
                break;
                case 180 :
                    WIND_DIRECTION = "S";
                break;
                case 270 :
                    WIND_DIRECTION = "W";
                break;
                case (d>0 && d<90) :
                    WIND_DIRECTION = "NE";
                break;
                case (d>90 && d<180) :
                    WIND_DIRECTION = "SE";
                break;
                case (d>180 && d<270) :
                    WIND_DIRECTION = "SW";
                break;
                case (d>270 && d<360) :
                    WIND_DIRECTION = "NW";
                break;
                default:
                    WIND_DIRECTION = "-";
                    break;
            }
    
            return WIND_DIRECTION;
        } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search