Search code examples
javascriptjquerygeolocationweather-apiyahoo-weather-api

Get local weather with only client-side script


I am making a JavaScript app which shows the weather of several places. Using the Yahoo Weather API, I easily get the data for several locations by manually giving WOEID. However I also want to get the weather of the current location of the user. Using the GeoLocation API, I can get the city name of the user; however Yahoo only works with its WOEID (city codes).

Can anyone guide me about the sort of approach I should take to get local weather of the user with only client-side script?

Note: I don’t want the user to enter/search their city name, I want to show it directly when theyloads the page.


Solution

  • It should be possible using this YQL Geo Library. This should work:

    yqlgeo.get('visitor', function(o) {
        if (o.error) {
            alert('Unable to determine user location');
        } else {
            alert('WOEID: ' + o.place.woeid);
        }
    });