I am testing out some code, just to play. Just to learn SwiftUI.
The idea: I want to see on a map, my location and I want to see the sunrise and sunset times. I found on GitHub a package dependency: SunKit
And I have a question. In Sunlit the standard location (if no location is found) is Apple HQ. I want to change that. I want to change my code so that It takes the location of user and that the SunKit dependency makes the sunrise and sunset times based on the users location.
Your help s appreciated How can I change the code?
import SwiftUI
import SunKit
import CoreLocation
struct ContentView: View {
let timeFormatter: DateFormatter = {
let tf = DateFormatter()
tf.dateFormat = "HH:mm"
return tf
}()
@State var location: CLLocation?
let timeZone = TimeZone.current
public init() {
self.location = CLLocationManager().location
}
var body: some View {
let sun: Sun
let timeZoneOffset = Double(timeZone.secondsFromGMT()) / 3600.0
let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
if let userLocation = locationManager.location {
sun = Sun(location: userLocation, timeZone: timeZoneOffset)
} else {
sun = Sun(location: CLLocation(latitude: 37.7749, longitude: -122.4194), timeZone: 0)
}
return VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
Text("Sunrise time: \(timeFormatter.string(from: sun.sunrise))")
Text("Azimuth: \(sun.sunriseAzimuth)°")
Text("Sunrise: \(sun.sunrise)")
Text("Location: \(sun.location)")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I tried the code and I was expecting that the location of the user would give the correct sunrise and sunset time.