Learn Courses My Dashboard

Help with getting Device Location

I am building a weather app. I’m just trying to get the current temperature wherever the user is. It kind of works with my hard-coded Chicago Location, although the temperature says 87 when it’s 52.

Instead of using the hardcoded value, I want to use the device location, but I can’t wrap my head around how to pass that data.

import WeatherKit
import CoreLocation

class RequestWeather:  NSObject, ObservableObject, CLLocationManagerDelegate {
    @Published private(set) var currentTemperature = String()
    @Published var location: CLLocationCoordinate2D?
    let manager = CLLocationManager()
    private let weatherService = WeatherService()
    private let chicago = CLLocation(latitude: 41.79632, longitude: 87.58293)
    override init() {
        manager.delegate = self
    func getDeviceLocation() {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        location = locations.first?.coordinate
    func getWeatherData() {
        Task {
            do {
                let weather = try await weatherService.weather(for: chicago)
                DispatchQueue.main.async {
                    self.currentTemperature = weather.currentWeather.temperature.formatted()
            } catch {
                print("Getting Date failed: \n\(error.localizedDescription)")

Hi @Davu

Have you tried watching SwiftUI Weather App created by Stewart Lynch? You may want to check it first.