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!)")
}
}
-
Answer # 1
Related articles
- firebase realtime database data sync not done (under certain conditions)
- i want to be able to automatically back up the data in firebase's realtime database
- xcode - about swift loading action
- reactjs - cannot push to realtime database
- i don't know how to retrieve a value from the swift uisearchbar database
- swift - i don't know how to retrieve the values of firebase and realtime database
Related questions
- swift : xcode. After user authorization in firebase, transition to view controller does not work
- swift - i want to retrieve the elements of an array nested in firestore
- ios - [swift] i want to save the image and video values in firebase storage and then save the url in cloud firestore
- analytics collection is not disabled on ios [firebase]
- swift - about firebase/rules
- swift - i want to resolve a build-time error in firebase/analytics
- xcode - i want to display images using firebase storage with swift
- ios - [swift] i want to register user name at the same time as signing up with firebaseauth
- i cannot turn on "push notifications" in xcode
- xcode - 'stpsourceprotocol' error [stripe]
I'm trying to retrieve a value saved as a string (String) as a number (Double).
Let's match it to one.