我正在尝试使用MVVM-Pattern实现SwiftUI和CoreLocation。我作为助手工作正常。但是,如何更改我的属性?我实现了我的in 。这是我的问题。LocationManager
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()
}
}
SwiftUI 2
StateObject
在这种情况下请改用
struct LocationView: View {
@StateObject var locationManager: LocationManager = LocationManager()
...
SwiftUI 1
LocationViewModel
在这里实际上是多余的。由于您LocationManager
是ObservableObject
您,因此可以在视图中直接使用它,如下所示:
struct LocationView: View {
@ObservedObject var locationManager: LocationManager = LocationManager()
var body: some View {
VStack(alignment: .leading) {
Text("Latitude: \(locationManager.location.coordinate.latitude.description)")
Text("Longitude: \(locationManager.location.coordinate.longitude.description)")
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句