【发布时间】:2018-12-28 06:24:51
【问题描述】:
我已经使用谷歌热图有一段时间了,我正在尝试将用户的位置以及其他一些项目存储在 firebase 中,然后在谷歌地图上显示它们。我一直在研究这种方法,但由于某种原因,我输入的代码不会存储用户的位置,但会存储所有其他信息,例如工作图片、工作类别、工作简历和价格。我想知道是否有人可以帮助我更正我的代码以显示如何将用户位置存储到 Firebase。
@objc func handlePost() {
let fromId = Auth.auth().currentUser!.uid
let timestamp = Int(Date().timeIntervalSince1970)
guard let image = JobImageView.image, let category = JobCategory.text, let description = JobBio.text, let cost = price.text else {
print("Error")
return
}
guard let uid = Auth.auth().currentUser?.uid else {
return
}
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("job_images").child("\(imageName).png")
if let JobImageUrl = self.JobImageView.image, let uploadData = JobImageUrl.jpegData(compressionQuality: 0.75) {
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil, metadata != nil {
print(error ?? "")
return
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
if let JobImageUrl = url?.absoluteString {
//if i attempt to store the users location, the line below crashes with the error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
let myLocation = self.mapView.myLocation
let coords: [String: Double] = [
"lat": myLocation!.coordinate.latitude,
"long": myLocation!.coordinate.longitude
]
let values = ["category": category, "description": description, "cost": cost, "JobImageUrl": JobImageUrl, "fromId": fromId, "timestamp": timestamp, "location": coords] as [String : Any]
self.registerUserIntoDatabseWithUID(uid: uid, values: values as [String : AnyObject])
}
})
})
}
}
private func registerUserIntoDatabseWithUID(uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference(fromURL: "https://odd-jobs-llc-f854a.firebaseio.com/")
let usersReference = ref.child("JobPost").child(uid)
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print("err")
return
}
self.dismiss(animated: true, completion: nil)
})
}
【问题讨论】:
-
如果在
registerUserIntoDatabseWithUID中打印values.coords,它会显示什么? -
@FrankvanPuffelen “[String : AnyObject]”类型的值没有成员“coords”
-
抱歉,应该是打印
values.location。 -
@FrankvanPuffelen 同样的错误..
-
有趣。由于您在调用
registerUserIntoDatabseWithUID之前将location添加到values数组中,因此我希望它会打印您添加的值。你有没有做进一步的调查? (请记住,Stack Overflow 是一个非常低效的交互式调试器)
标签: ios firebase google-maps firebase-realtime-database heatmap