Я пытаюсь реализовать SwiftUI и CoreLocation с MVVM-Pattern . Мой LocationManager
как Helper работает нормально. Но как я могу изменить свойства моего LocationViewModel
? Я реализовал мой @ObservedObject
из LocationManager
в LocationViewModel
. Здесь моя проблема.
У меня нет идеи реализовать свойства, которые они меняют на лету. Ничего не изменилось в моем LocationView. При нажатии кнопки все работает один раз. Но LocationViewModel
должен изменять свои свойства при каждом изменении LocationManager
.
В заключение я хотел бы отобразить текущую позицию пользователя.
// Location Manager as Helper
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject {
let locationManager = CLLocationManager()
let geoCoder = CLGeocoder()
@Published var location: CLLocation?
@Published var placemark: CLPlacemark?
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func geoCode(with location: CLLocation) {
geoCoder.reverseGeocodeLocation(location) { (placemark, error) in
if error != nil {
print(error!.localizedDescription)
} else {
self.placemark = placemark?.first
}
}
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
DispatchQueue.main.async {
self.location = location
self.geoCode(with: location)
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// TODO
}
}
// Location Model
import Foundation
import CoreLocation
struct Location {
var location: CLLocation = CLLocation()
var placemark: CLPlacemark = CLPlacemark()
}
// Location View Model
import SwiftUI
import CoreLocation
class LocationViewModel: ObservableObject {
@ObservedObject var locationManager: LocationManager = LocationManager()
@Published var location: Location
init() {
self.location = Location()
}
}
// Location View
import SwiftUI
struct LocationView: View {
@ObservedObject var locationViewModel: LocationViewModel = LocationViewModel()
var body: some View {
VStack(alignment: .leading) {
Text("Latitude: \(self.locationViewModel.location.location.coordinate.latitude.description)")
Text("Longitude: \(self.locationViewModel.location.location.coordinate.longitude.description)")
}
}
}
struct LocationView_Previews: PreviewProvider {
static var previews: some View {
LocationView()
}
}
Обновление
Теперь я настроил MapView .
Но как мне получить данные моего LocationManager
? Метод didUpdateLocations
работает в LocationManager
.
Все, что я пытаюсь сделать, идет не так. Я хотел бы установить регион на MapView
в зависимости от текущего местоположения пользователя. В UIKit это было очень просто, но в SwiftUI это странно.
// Map View
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
@ObservedObject var locationManager: LocationManager = LocationManager()
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ control: MapView) {
self.parent = control
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: .zero)
mapView.delegate = context.coordinator
return mapView
}
func updateUIView(_ mapView: MKMapView, context: Context) {
mapView.showsUserLocation = true
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView()
}
}