0

I have location manager configured similar to how this post described How to get Current Location using SwiftUI, without ViewControllers? and I can see in the console that the lat and long are being returned.

However, when I when try to set the coordinates here I get back 0,0

class DownloadWeatherLocation: ObservableObject {

    @Published var weatherlocationdata: [WeatherLocationDecoder] = []
    let locationManager = LocationManager()

    @AppStorage("userWID") var userWID = 0
    @AppStorage("userLocationCity") var userLocationCity = ""

    var weatherDataCancellabes = Set<AnyCancellable>()

    init() {

 I suspect the issue is in here.

        print("WeatherLocation: loading init")
        var userLatitude: String {
             return "\(locationManager.lastLocation?.coordinate.latitude ?? 0)"
         }

         var userLongitude: String {
             return "\(locationManager.lastLocation?.coordinate.longitude ?? 0)"
         }

        getWeatherLocation(weatherlocationUrl: "https://www.metaweather.com/api/location/search/?lattlong=\(userLatitude),\(userLongitude)")
    }

... function that calls the URL which works, but uses 0,0

}
Robert
  • 809
  • 3
  • 10
  • 19
  • You're initializing `locationManager` and are trying to access `lastLocation` before asking the `locationManager` to find the user's location... The question you linked to has a very comprehensive answer, and I don't see you using any of it. – EmilioPelaez Sep 13 '21 at 16:37
  • Well that works if I just want to display the coordinates, but they're not getting passed into the function. I can also see this when I set the coordinates into a state. `userLat = "\(locationManager.lastLocation?.coordinate.longitude ?? 0)"` – Robert Sep 13 '21 at 16:54

0 Answers0