noazark / weather

real weather for Javascript
MIT License
229 stars 109 forks source link

Using ID instead of city name #29

Closed SmilingTexan closed 4 years ago

SmilingTexan commented 6 years ago

According to openweathermap, you should use the city ID instead of name to get a more exact match; I've modified the following functions to allow for this:

Weather.getCurrentById = function( id, callback ) {
    var url = "http://api.openweathermap.org/data/2.5/forecast?id=" + encodeURIComponent( id ) + "&cnt=1";

    if ( Weather.APIKEY ) {
        url = url + "&APPID=" + Weather.APIKEY;
    } else {
        console.log( 'WARNING: You must set an apiKey for openweathermap' );
    }

    return this._getJSON( url, function( data ) {
        callback( new Weather.Current( data ) );
    } );
};

and

Weather.getForecastById = function( id, callback ) {
    var url = "http://api.openweathermap.org/data/2.5/forecast?id=" + encodeURIComponent( id ) + "&cnt=1";

    if ( Weather.APIKEY ) {
        url = url + "&APPID=" + Weather.APIKEY;
    } else {
        console.log( 'WARNING: You must set an apiKey for openweathermap' );
    }

    return this._getJSON( url, function( data ) {
        callback( new Weather.Current( data ) );
    } );
};
codewithtyler commented 4 years ago

To clarify OpenWeatherMap recommends using the city id because using the city name sometimes causes issues since some cities show up more than once in the list (i.e. Kansas City). If you just use the city name they have to figure out which one you meant and return the results for that. I'm not 100% but I believe this happens because people can add their own weather stations in their homes to report data. I can only assume that when you add a new weather station like that it adds the city an additional time to the list.

I'm about to begin working on this. So it should be resolved shortly.