how to find weather forecast city id?
Asked Answered
S

4

6
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?

Shreeves answered 11/1, 2015 at 3:51 Comment(1)
do you aleady have a solution?Colincolinson
I
4

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

Iberian answered 11/1, 2015 at 3:58 Comment(1)
Both links are dead... It seems like the last valid link to the page is web.archive.org/web/20180619015316/http://openweathermap.org/… from web.archive.org/web/20180601000000*/openweathermap.org/help/city_list.txtHypercritical
P
3

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.

Penstemon answered 22/7, 2015 at 4:0 Comment(6)
is there API to download the city list instead downloading the static file?Passible
@Passible 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.Penstemon
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.Passible
@Passible the file is huge, zipped; receiving it as a JSON/XML text response would be prohibitive.Wisecrack
Pagination can be used. List of countries - it's cities etcPassible
Is there some options to filter for certain countries only? Let's say get all available cities in Germany?Hypercritical
V
2

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

Ventose answered 21/4, 2019 at 10:18 Comment(3)
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.Belittle
Hello mate, that's an outstanding initiative. How to get API Key for the API @BelittleRunt
@Runt You need to register on the website, and then create an API key for your accountBelittle
K
1

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

Kipper answered 21/4, 2017 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.