1

I am building a SwiftUI app that shows data based on user lat/long. I have based my code off of this sample provided by the framework dev.

With SwiftUI I have my LocationManager set as:

class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
  
    @Published var userLatitude: Double = 0.0
    @Published var userLongitude: Double = 0.0
  
  private let locationManager = CLLocationManager()
  
  override init() {
    super.init()
    self.locationManager.delegate = self
    self.locationManager.distanceFilter = 100.0
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
  }

  
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }
    userLatitude = location.coordinate.latitude
    userLongitude = location.coordinate.longitude
    print("Hello I'm here! \(location)")
  }
}

Whenever I go back to my ContentView and try to read the Lat/Long it just shows up as 0.0. but if I output them within the body the values show up correctly.

struct ContentView: View {
    @State private var times = prayerTimes()
 @ObservedObject var locationViewModel = LocationViewModel()
    var body: some View {
        NavigationView {
            PrayerTimeView(times: $times)
                .navigationBarTitle(Text("Prayer Times"))
        }
    }

    static func prayerTimes() -> PrayerTimes? {
        let cal = Calendar(identifier: Calendar.Identifier.gregorian)
        let date = cal.dateComponents([.year, .month, .day], from: Date())
        let coordinates = Coordinates(latitude: locationViewMode.userLatitude, longitude: locationViewMode.userLongitude)
        var params = CalculationMethod.moonsightingCommittee.params
        params.madhab = .hanafi
        return PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params)
    }
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Have you implemented everything else that goes on with location such as getting permission, setting the `Info.plist` keys, and handling errors? If you haven't you need to set this up before getting location updates. https://developer.apple.com/documentation/corelocation – lorem ipsum Nov 21 '20 at 20:38
  • @loremipsum Yes, I have added that. The lat/long do show up in the body, but not inside the static func. – Efrain Ayllon Nov 21 '20 at 21:34
  • The wrapper update functions only work in Views not in other forms of code. Try putting the function in the ObservableObject. Then pass the elements to a View – lorem ipsum Nov 22 '20 at 00:11

1 Answers1

1

prayerTimes() only call once when you init the view. Why don't you make times as a @Published of your ViewModel. When location changes, just update that value.

PrayerTimeView(times: $viewmodel.times)
                .navigationBarTitle(Text("Prayer Times"))
Quang Hà
  • 4,613
  • 3
  • 24
  • 40