在屏幕上显示用户位置时出现问题
Problem displaying user location on screen
我试图在屏幕上显示用户位置坐标,但是当我 运行 应用程序时没有任何显示,尽管我可以在控制台中看到坐标。我遇到的第二个问题是,当我按下停止按钮时,位置更新不会停止。
位置管理器:
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate{
let locationManager = CLLocationManager()
@Published var location: CLLocationCoordinate2D?
override init(){
super.init()
locationManager.delegate = self
}
func startTracking() {
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
func stopTracking() {
print("stop test")
locationManager.stopUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = false
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let tempLocation = locations.last?.coordinate
print(tempLocation)
DispatchQueue.main.async {
self.location = tempLocation
}
}
}
查看:
import SwiftUI
struct ContentView: View {
@StateObject var locationManager = LocationManager()
var body: some View {
VStack{
startLocationTrackingButton()
stopLocationTrackingButton()
if let location = locationManager.location {
Text("Your location: \(location.latitude), \(location.longitude)")
}
}
}
}
struct startLocationTrackingButton: View{
@StateObject var locationManager = LocationManager()
var body: some View{
Button("START"){
locationManager.startTracking()
}
}
}
struct stopLocationTrackingButton: View{
@StateObject var locationManager = LocationManager()
var body: some View{
Button("STOP"){
locationManager.stopTracking()
}
}
}
感谢帮助!
你的每一个观点都有:
@StateObject var locationManager = LocationManager()
您正在为每一个创建一个不同的 位置管理器。尝试这样做:
truct ContentView: View {
@StateObject var locationManager = LocationManager()
var body: some View {
VStack{
StartLocationTrackingButton(manager: locationManager)
StopLocationTrackingButton(manager: locationManager)
if let location = locationManager.location {
Text("Your location: \(location.latitude), \(location.longitude)")
}
}
}
}
struct StartLocationTrackingButton: View{
@ObservedObject var manager: LocationManager
var body: some View{
Button("START"){
manager.startTracking()
}
}
}
struct stopLocationTrackingButton: View{
@ObservedObject var manager: LocationManager
var body: some View{
Button("STOP"){
manager.stopTracking()
}
}
}
仅在创建和拥有对象时使用 @StateObject
。如果您将 ObservableObject
传递给另一个视图,则该视图应将其声明为 @ObservedObject
并且不要为其提供初始值。
我试图在屏幕上显示用户位置坐标,但是当我 运行 应用程序时没有任何显示,尽管我可以在控制台中看到坐标。我遇到的第二个问题是,当我按下停止按钮时,位置更新不会停止。
位置管理器:
import Foundation
import CoreLocation
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate{
let locationManager = CLLocationManager()
@Published var location: CLLocationCoordinate2D?
override init(){
super.init()
locationManager.delegate = self
}
func startTracking() {
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startUpdatingLocation()
}
func stopTracking() {
print("stop test")
locationManager.stopUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = false
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let tempLocation = locations.last?.coordinate
print(tempLocation)
DispatchQueue.main.async {
self.location = tempLocation
}
}
}
查看:
import SwiftUI
struct ContentView: View {
@StateObject var locationManager = LocationManager()
var body: some View {
VStack{
startLocationTrackingButton()
stopLocationTrackingButton()
if let location = locationManager.location {
Text("Your location: \(location.latitude), \(location.longitude)")
}
}
}
}
struct startLocationTrackingButton: View{
@StateObject var locationManager = LocationManager()
var body: some View{
Button("START"){
locationManager.startTracking()
}
}
}
struct stopLocationTrackingButton: View{
@StateObject var locationManager = LocationManager()
var body: some View{
Button("STOP"){
locationManager.stopTracking()
}
}
}
感谢帮助!
你的每一个观点都有:
@StateObject var locationManager = LocationManager()
您正在为每一个创建一个不同的 位置管理器。尝试这样做:
truct ContentView: View {
@StateObject var locationManager = LocationManager()
var body: some View {
VStack{
StartLocationTrackingButton(manager: locationManager)
StopLocationTrackingButton(manager: locationManager)
if let location = locationManager.location {
Text("Your location: \(location.latitude), \(location.longitude)")
}
}
}
}
struct StartLocationTrackingButton: View{
@ObservedObject var manager: LocationManager
var body: some View{
Button("START"){
manager.startTracking()
}
}
}
struct stopLocationTrackingButton: View{
@ObservedObject var manager: LocationManager
var body: some View{
Button("STOP"){
manager.stopTracking()
}
}
}
仅在创建和拥有对象时使用 @StateObject
。如果您将 ObservableObject
传递给另一个视图,则该视图应将其声明为 @ObservedObject
并且不要为其提供初始值。