skip to Main Content

Wondering if anyone knows a solution to this –
I’m building a (semi) simple weather app with OpenWeatherMap using swiftUI. Basically finishing up and adding some last touches. Would like to be able let users choose between cities if there is more than one city with the same name, but as far as I can tell the only access to a full list of city names OpenWeatherMap offer is a zipped JSON, there’s no api call. And its way too large to include in the bundle.

heres the link to the list – http://bulk.openweathermap.org/sample/

As far as I can tell, without being able to check a list of cities – it seems the only way to specify the right city is to add a comma and then country code in the search. e.g "rome" returns a location in the US, "rome, it" returns Rome, Italy. Which isn’t a very good user experience.

Am I being stupid and missing something?

2

Answers


  1. Hey in order to get that list , You have to programmatically create a tableview which will be the half of height of that main view(set it initially to be hidden) and it should be visible when someone interacts with the searchBar or TextField (you’ll need to make it visible in one of the delegate methods of tableview or searchBar , whatever you are using)
    and render the city names in that tableview , that’s it , it will be easy if you know a bit of Programmatic UI if not watch some YouTube videos by Sean Allen on programmatic UI and you should be good to Go !! Best of luck

    Login or Signup to reply.
  2. Perhaps the OpenWeather Geocoding API was not available in 2021 but it is now. You can use this API to search for cities around the world from their weather database.

    https://openweathermap.org/api/geocoding-api

    If you query only the city name (without state and/or country) it returns a list of matches. For example if you search for "Dublin" and limit to 5 results, it returns Dublin, Ireland and 4 cities named Dublin in the USA. Then the user can select from a list to confirm the correct city.

    http://api.openweathermap.org/geo/1.0/direct?q={city name},{state code},{country code}&limit={limit}&appid={API key}
    

    This seems to be their recommended way to get the latitude and longitude of a city by name to then query the its forecast.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search