我想将一组纬度和经度坐标保存到Firebase,但每当我的应用程序运行此代码时,它都会崩溃!
let userID: String = (FIRAuth.auth()?.currentUser?.uid)!
let lat: Double = (locationManager.location?.coordinate.latitude)!
let lon: Double = (locationManager.location?.coordinate.longitude)!
self.ref.child("Location").child(userID).setValue(["Latitude": lat, "Longitude": lon])
无论如何我能解决这个问题并妥善保存吗?
答案 0 :(得分:1)
确保两件事: -
1。)你的viewController继承自CLLocationManagerDelegate
,并在delegate
viewDidLoad
设置为self
2。)您的info.plist
已将这些键添加到其中: -
NSLocationAlwaysUsageDescription ---我需要位置
NSLocationWhenInUseUsageDescription ---我需要位置
隐私 - 位置使用说明---我需要位置
class MapViewController: UIViewController , CLLocationManagerDelegate...{
...
..
let locationManager = CLLocationManager()
func viewDidLoad(){
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation()
}
//Call these functions for updating the getting the values of current location
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
print("authorised call came . . . . ")
mapView.myLocationEnabled = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("target position : = \(location.coordinate)")
print(locationManager.location!.coordinate.latitude)
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
self.FIRDatabase.database().reference().child("Location").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Latitude": locationManager.location!.coordinate.latitude, "Longitude": locationManager.location!.coordinate.longitude])
}
}
}