在屏幕上显示用户位置时出现问题

Posted

技术标签:

【中文标题】在屏幕上显示用户位置时出现问题【英文标题】:Problem displaying user location on screen 【发布时间】:2021-07-16 14:30:45 【问题描述】:

我正在尝试在屏幕上显示用户位置坐标,但当我运行应用程序时没有任何显示,即使我可以在控制台中看到坐标。我遇到的第二个问题是当我按下停止按钮时,位置更新不会停止。

位置管理器:

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()
        
    

感谢您的帮助!

【问题讨论】:

【参考方案1】:

您的每个视图都有:

@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,并且不要给它一个初始值。

【讨论】:

以上是关于在屏幕上显示用户位置时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

在 UITabBarController 中首次启动时出现自动布局问题

下载大量图像时出现内存泄漏问题

尝试在 Android 中运行 Google 地图时出现浅棕色屏幕

从通知启动主屏幕时出现 ANR

在 ESRI 地图中显示弹出窗口时出现问题

在地图视图上使用轨迹位置时出现可变错误