weatherKit

Hey everyone, I am working on a weather app using weatherKit on SwiftUI and I have everything set up such as the current weather, ten-day and hourly forecast. My only question is that I want to have the feature to search for a city that I want to add and see the weather information for a specific Country/city. How do I add an autocomplete search field. Is it possible to do it using weatherKit? Or its something completely different thing that I need to add. Thank you.

The WeatherKit API only needs you to provide a CLLocation object, nothing about the name of a city/country. You will have to use your own list of city/country names and get the locations of these to pass to WeatherKit.

Helpfully, CLGeocoder has that functionality already. Here is an example of how to use it:

func location(for address: String) async throws -> CLLocation? {
    let placemarks = try await CLGeocoder().geocodeAddressString(address)
    return placemarks.first?.location
}

if let londonLocation = try? await location(for: "London") {
    // pass londonLocation to WeatherKit

    let coordinates = londonLocation.coordinate
    print(coordinates) // CLLocationCoordinate2D(latitude: 51.5033466, longitude: -0.0793965)
}
weatherKit
 
 
Q