我是一个初学者,试图从LocationManager获取纬度和经度,并使用SwiftUI在ContentView中显示,但结果显示为零。但是控制台会打印LocationManager类中的数据。不显示内容视图中的纬度和经度。有人能帮忙吗?(Xcode 11(
这是LocationManager类
import Foundation
import CoreLocation
import Combine
class LocationManager: NSObject,CLLocationManagerDelegate, ObservableObject {
private let manager: CLLocationManager
var willChange = PassthroughSubject<LocationManager, Never>()
var getLat: String = ""
var getLon: String = ""
var lastKnownLocation: CLLocation? {
willSet {
willChange.send(self)
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .denied {
print("denied")
}
else{
print("athorized")
manager.requestLocation()
}
}
func start() {
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
init(manager: CLLocationManager = CLLocationManager()) {
self.manager = manager
super.init()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func startUpdating() {
self.manager.delegate = self
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//print(locations)
lastKnownLocation = locations.last
getLat = "(locations.last!.coordinate.latitude)"
getLon = "(locations.last!.coordinate.longitude)"
showLocation()
}
func showLocation() {
print("from showLocation")
print("Latitude: (getLat)")
print("Longitude: (getLon)")
}
}
这是显示LocationManager类的纬度和经度的控制台
这是内容视图
import SwiftUI
import CoreLocation
struct ContentView: View {
@State var managerDelegate = LocationManager()
@State var manager = CLLocationManager()
@ObservedObject var location = LocationManager()
var lat: String {
return "(location.lastKnownLocation?.coordinate.latitude ?? 0.0)"
}
var lon: String {
return "(location.lastKnownLocation?.coordinate.longitude ?? 0.0)"
}
init() {
self.manager.delegate = self.managerDelegate
do {
try self.manager.requestAlwaysAuthorization()
}
catch {
print(error.localizedDescription)
self.manager.requestAlwaysAuthorization()
}
}
var body: some View {
VStack {
Text("Hello, World")
Text("Latitude: (lat)")
Text("Longitude: (lon)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是模拟器中ContentView的输出
好吧,我发现自己有时间阅读您的代码,我有一个解决方案。
首先,您需要将委托设置为ContentView.init()
:中您正在观察的变量
self.manager.delegate = self.location
之后,您需要立即调用在LocationManager
:中定义的startUpdating()
函数
self.location.startUpdating()
最后,您可能想要删除will-change
实现,并选择更简单的:
@Published var lastKnownLocation: CLLocation?
已发布的包装器将自动为您触发对象更改。
Refactor
现在,如果您愿意,还可以将init语句缩减为一行:
init() {
self.location.startUpdating()
}
这意味着您可以自由删除几个属性:
`@State var managerDelegate = LocationManager()`
`@State var manager = CLLocationManager()`
TLDR这是代码的缩短版本:
内容视图
import SwiftUI
struct ContentView: View {
@ObservedObject var location = LocationManager()
var lat: String {
return "(location.lastKnownLocation?.coordinate.latitude ?? 0.0)"
}
var lon: String {
return "(location.lastKnownLocation?.coordinate.longitude ?? 0.0)"
}
init() {
self.location.startUpdating()
}
var body: some View {
VStack {
Text("Location breakdown")
Text("Latitude: (lat)")
Text("Longitude: (lon)")
}
.padding()
.padding()
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 10)
}
}
仓位经理
import Foundation
import CoreLocation
class LocationManager: NSObject, CLLocationManagerDelegate, ObservableObject {
private let manager = CLLocationManager()
@Published var lastKnownLocation: CLLocation?
func startUpdating() {
self.manager.delegate = self
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastKnownLocation = locations.last
}
}