Home>

The coordinates cannot be read from the Realtime Database as a Double type. (The read value is displayed as nil in the console) There is no error. Please tell me what is wrong.

class FirstViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var latitude: UILabel!
    @IBOutlet weak var longitude: UILabel!
    let user = Auth.auth (). currentUser? .uid
    let locationManager = CLLocationManager ()
    var ref: DatabaseReference!
    var otherUserLatitude: Double? = nil
    var otherUserLongitude: Double? = nil
    override func viewDidLoad () {
        super.viewDidLoad ()
        ref = Database.database (). reference ()
        locationManager.delegate = self as! CLLocationManagerDelegate
    }
    // going well
    @IBAction func userLocation (_ sender: Any)
    {
        let refLatitude = ref.child ("userLatitude")
        refLatitude.setValue (latitude.text)
        let refLongitude = ref.child ("userLongitude")
        refLongitude.setValue (longitude.text)
        print ("Location information saved")
    }
    // cannot get from DB
    @IBAction func othersLocation (_ sender: Any)
    {
        ref.child ("userLatitude"). observeSingleEvent (of: .value, with: {(snapshot) in
        let value = snapshot.value as? NSDictionary
        let otherUserLatitude = value? ["userLatitude"] as? Double
        // Console display otherUserLatitude: nil
        print ("otherUserLatitude:", otherUserLatitude)
        })
        ref.child ("userLongitude"). observeSingleEvent (of: .value, with: {(snapshot) in
        let value = snapshot.value as? NSDictionary
        let otherUserLongitude = value? ["userLongitude"] as? Double
        // Console display otherUserLongitude: nil
        print ("otherUserLongitude:", otherUserLongitude)
        })
    }
    func locationManager (_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization ()
        case .authorizedWhenInUse:
            locationManager.startUpdatingLocation ()
        default:
            break
        }
    }
    func locationManager (_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let coordinate = locations.last? .coordinate {
            // Zoom in on your current location
            let span = MKCoordinateSpan (latitudeDelta: 0.01, longitudeDelta: 0.01)
            let region = MKCoordinateRegion (center: coordinate, span: span)
            mapView.region = region
        }
        guard let newLocation = locations.last else {
                return
        }
        self.latitude.text = "" .appendingFormat ("%. 4f", newLocation.coordinate.latitude)
        self.longitude.text = "" .appendingFormat ("%. 4f", newLocation.coordinate.longitude)
        let location = locations.first
        let latitude = location? .coordinate.latitude
        let longitude = location? .coordinate.longitude
        print ("latitude: \ (latitude!) \ nlongitude: \ (longitude!)")
    }

}