繁体   English   中英

FireStore 根据用户位置显示自定义信息

[英]Display custom information according to user location FireStore

我试图根据userLocation显示自定义信息(ig,ft用户在洛杉矶,他会得到Text(“X”)。如果他在圣地亚哥,他会得到Tex(“Y”))。 为此,我正在使用 Firestore。 在那里,我有一个名为 TEST --> auto ID --> "Name": "Test", Los Angeles::true 的集合

这就是我获得 userLocation 的方式:

class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate{
    @Published var locationManager = CLLocationManager()
    @Published var userLocation : CLLocation!
    @Published var userAddress = ""
    @Published var noLocation = false
    
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .authorizedWhenInUse:
            self.noLocation = false
            manager.requestLocation()
        case .denied:
            self.noLocation = true
        default:
            self.noLocation = false
            locationManager.requestWhenInUseAuthorization()
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.userLocation = locations.last
        self.extractLocation()
    }
    
    func extractLocation(){
        CLGeocoder().reverseGeocodeLocation(self.userLocation) { (res, err) in
            guard let safeData = res else {return}
            
            var address = ""
            
         //   address += safeData.first?.name ?? ""
         //   address += ", "
            address += safeData.first?.locality ?? ""
            
            
            self.userAddress = address
       print(address)
        }
    }  
}

然后,我有这个代码来获取文档:

struct Testtt: Identifiable{
    var id: String = UUID().uuidString
    var name: String
   
    }
    class TesteViewModel: NSObject,ObservableObject{
    @StateObject var LocationModel = LocationViewModel()
    let db = Firestore.firestore()
    @Published var testsss = [Testtt]()
    
    
    func testeApp(){
        db.collection("Test").whereField(LocationModel.userAddress, isEqualTo: true).getDocuments { (querySnapshot, err) in
            guard let documents = querySnapshot?.documents else {return }
            self.testsss = documents.map { (queryDocumentSnapshot) -> Testtt in
               let data = queryDocumentSnapshot.data()
                
                let name = data["Name"] as? String ?? ""
                
                return Testtt(name: name)
            }
        }
        
    }
    
}
  • 之前,我手动添加了位置而不是LocationModel.userAddress ,并且它工作正常。 但是,在使用它之后,该项目启动了,但出现了 2 个错误:

紫色错误:

无需安装在视图上即可访问 StateObject 的对象。 这将每次创建一个新实例。

线程 1 错误:

无效的字段路径 ()。 路径不得为空、以 '.' 开头、以 '.' 结尾或包含 '..'"`

在调试时我得到:

由于未捕获的异常“FIRInvalidArgumentException”而终止应用程序,原因:“无效的字段路径 ()”。 路径不得为空、以“.”开头、以“.”结尾或包含“..”

在我按下将我带到此类视图的按钮后,应用程序立即崩溃

如果有人有更多信息的链接,我将不胜感激receivw

您能在使用之前检查LocationModel.userAddress是否具有正确的内容吗? 如果该字符串为空或 null,则可能导致此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM