Reputation: 61
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?
Upvotes: 6
Views: 36925
Reputation: 41
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
Upvotes: 1
Reputation: 3201
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
Upvotes: 2
Reputation: 1066
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.
Upvotes: 3
Reputation: 2621
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
Upvotes: 4