【问题标题】:Store coordinate to real time Database in Firebase将坐标存储到 Firebase 中的实时数据库
【发布时间】:2018-11-08 23:55:54
【问题描述】:

我正在尝试保存纬度。并且很长。到数据库。但是数据库没有显示任何内容,也没有在 Swift 中显示任何错误。我的代码如下所示。

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.showsUserLocation = true

        if CLLocationManager.locationServicesEnabled() == true {
        if CLLocationManager.authorizationStatus() == .restricted || CLLocationManager.authorizationStatus() == .denied || CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager.requestWhenInUseAuthorization()
        }

    let userLatitude = locationManager.location?.coordinate.latitude
    let userLongtitude = locationManager.location?.coordinate.longitude
    let userTime = locationManager.location?.timestamp

    Auth.auth().createUser(withEmail: "test@gmail.com",password: "xxxx", completion: {(User, error) in
        if error != nil {
            self.displayMyalertMessage(userMessage: error!.localizedDescription)
        } else {
            print("Success")
        }
        guard let uid = User?.user.uid else {return}
        let ref = Database.database().reference(fromURL:"")
        let userReference = ref.child("Locations").child(uid)

        let values = ["Latitude": userLatitude as Any, "Longtitude": userLongtitude as Any, "Time": UserTime as Any] as [String : Any]
        userReference.updateChildValues(values, withCompletionBlock: { (error, reference) in
            if error != nil {
                print(error as Any)
                return
            }
        })   
    })
}

【问题讨论】:

    标签: swift firebase firebase-realtime-database core-location


    【解决方案1】:
    var locationManager = CLLocationManager()
    
    func initiateLocation(){
    
        locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            locationManager.distanceFilter = 100
            locationManager.startUpdatingLocation()
            locationManager.allowsBackgroundLocationUpdates = true
    
            //checkForLocationServices()
            checkLocationAuthorizationStatus()
        guard let myLatitude = locationManager.location?.coordinate.latitude else{
            return
        }
        guard let myLongitude = locationManager.location?.coordinate.longitude else{
            return
        }
    
    }
    
    
    func checkLocationAuthorizationStatus() {
        let status = CLLocationManager.authorizationStatus()
        if status == CLAuthorizationStatus.notDetermined{
            print("NotDetermined")
            locationManager.requestWhenInUseAuthorization()
            CLLocationManager.locationServicesEnabled()
            locationManager.requestLocation()
        }else {
            print("Problem with authorization")
        }
    }
    
    
    
    // Handle incoming location events.
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        print("Location: \(location)")
        updateMyLocationToDataBase()
    }
    
    
    // Handle authorization for the location manager.
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .restricted:
            print("Location access was restricted.")
        case .denied:
            print("User denied access to location.")
            // Display the map using the default location.
        case .notDetermined:
            print("Location status not determined.")
        case .authorizedAlways: fallthrough
        case .authorizedWhenInUse:
            print("Location status is OK.")            
        }
    }
    
    // Handle location manager errors.
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        locationManager.stopUpdatingLocation()
        print("Error: \(error)")
    
        let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
    
        let msg :String = "You have denied the app to access your location. Please enable the location services in your settings for the app to get the location";
        let alertController = UIAlertController(title: "Allow \(appName) to access your location while you are using the app?", message: msg, preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.default, handler: nil)
    
        let settingsAction = UIAlertAction(title: "SETTINGS", style: .default) { (_) -> Void in
            guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
    
            if UIApplication.shared.canOpenURL(settingsUrl) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            }
        }
    
    
        alertController.addAction(cancelAction)
        alertController.addAction(settingsAction)
    
        self.present(alertController, animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2019-09-06
      • 1970-01-01
      • 2019-08-10
      • 2018-11-22
      • 2019-04-07
      相关资源
      最近更新 更多