how to find weather forecast city id?

30,448

Solution 1

There is a similar question that was asked in the OpenWeatherMap support center. Here is the link OpenWeatherMap

Also, here is a link to the list of cities and their respective ID's (just CTRL + F to find the city you want): List of Cities & IDs

Solution 2

A full list of CityID's can be downloaded from: http://bulk.openweathermap.org/sample/city.list.json.gz

You can manually search that file (after unzipping it) for your city name or search from the shell like:

grep -i "london" city.list.json

Regarding the "q" vs. "id" query tags: you can search by city (and country) using the "q" tag like this:

http://api.openweathermap.org/data/2.5/forecast/daily?q=London,GB

(note that the country code isn't necessarily required)

and that works as long as there is no question about which city. If there's multiple cities of the same name in a country, though, you can use the CityID to specify a certain one after you've found the correct identifier in the list linked above like this:

http://api.openweathermap.org/data/2.5/forecast/daily?id=2643743

which also returns London's weather, but ensures you don't accidentally get the weather for London, Kentucky, USA or London, Ohio, USA if you've chosen not to use the country code in the "q" tag.

Solution 3

With openweathermap you need to make an account here . After that you can generate an API key which will be used in the address bar.... like this:

http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=*******************************

just replace stars with generated key

Solution 4

Sounds like your app needs to search for a city as the user types in the city name. You can do that by running the OpenWeatherMap City Finder server on some kind of hosting (on Heroku, on Docker hostings, maybe purchase your own cheapest VM from Digital Ocean for $5 per month), then use the OWM City Finder Client to access that server and resolve any string that user inputted into a JSON array of cities, with GPS and IDs.

The OWM City Finder Server requires no setup, simply run the following on target machine:

docker run --rm -ti -p25314:25314 mvysny/owm-city-finder-server:0.1

To test the server and perform a query for a city, just run:

$ curl localhost:25314/city?query=helsinki
[{"id":658226,"name":"Helsinki","country":"FI","coord":{"lon":24.93417,"lat":60.17556}},{"id":658225,"name":"Helsinki","country":"FI","coord":{"lon":24.93545,"lat":60.169521}},{"id":658224,"name":"Helsinki","country":"FI","coord":{"lon":21.438101,"lat":60.60778}}]

You can access the REST directly from your app, or you can use the provided client for even easier access. Read more at https://gitlab.com/mvysny/owm-city-finder/tree/master/owm-city-finder-server

Share:
30,448
Admin
Author by

Admin

Updated on April 08, 2020

Comments

  • Admin
    Admin about 4 years
    final String FORECAST_BASE_URL =
            "http://api.openweathermap.org/data/2.5/forecast/daily?";
    final String QUERY_PARAM = "q";
    

    Hello. I'm making a weather forecast app. But when I use "http://openweathermap.org"'s API, I stock in trouble how can I get the city code or ID "q"? I try to find the city ID but I can't.

    Example code Call by city ID:

    api.openweathermap.org/data/2.5/forecast/daily?id=524901
    

    and I heard that "q" is used to "id" also. I mean

    api.openweathermap.org/data/2.5/forecast/daily?q=524901
    

    is also possible. i want to do like this.

    So, How to find city id? Anybody knows?

  • gammay
    gammay over 4 years
    is there API to download the city list instead downloading the static file?
  • Anson VanDoren
    Anson VanDoren over 4 years
    @gammay I'm not sure exactly what you're looking for, but if you're asking if there is an API call that will return a response containing all the cities known to OpenWeatherMap, then no, there isn't (as far as I can tell). The list of cities should rarely change, so downloading the list once should be sufficient for most scripts. I may be able to help further if you can expand on what exactly you need to accomplish.
  • gammay
    gammay over 4 years
    You answered my question. Thank you. I am not convinced downloading city list once is sufficient as city/country names change (though not very frequently). Also my opinion is old & new names should work as people may use either.
  • matt
    matt about 4 years
    @gammay the file is huge, zipped; receiving it as a JSON/XML text response would be prohibitive.
  • gammay
    gammay about 4 years
    Pagination can be used. List of countries - it's cities etc
  • FranzHuber23
    FranzHuber23 almost 4 years
  • FranzHuber23
    FranzHuber23 almost 4 years
    Is there some options to filter for certain countries only? Let's say get all available cities in Germany?
  • InterLinked
    InterLinked almost 2 years
    If you want to get city IDs from cities, without running your own container or web server, you can also use an API I've put up for this purpose (Weather City API): api.interlinked.us . Works much better than trying to work with an unwieldy JSON file, too.