【问题标题】:Getting coordinates from Firebase to make annotations从 Firebase 获取坐标以进行注释
【发布时间】:2017-11-09 03:44:52
【问题描述】:

我目前正在尝试从 firebase 获取我的数据并在我的 MKMapKitView 中创建注释。我相信我正在正确检索数据,但没有正确创建注释。

我认为,因为有多个用户,我不能将其设置为常规的注释方式。

   let   userLocation = CLLocationCoordinate2D(latitude: Double(userLatitude!), longitude: Double(userLongitude!))

                let userAnnotation = MKPointAnnotation();
        userAnnotation.coordinate = self.userLocation!;
                //userAnnotation.title = "Riders Location";
                map.addAnnotation(userAnnotation);

        }

我还将添加检索用户的方式。

func retrieveUsers(){
        let ref = Database.database().reference()
        ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
            let users = snapshot.value as! [String: AnyObject]
            self.user.removeAll()
            for (_,value) in users {
                if let uid = value["uid"] as? String {
                    if uid != Auth.auth().currentUser!.uid {
                        let userToShow = User()
                        if let fullName = value["full name"] as? String,
                            let userLongitude = value["long"] as? Double,
                            let userLatitude = value["lat"] as? Double


                        {
                            userToShow.fullName = value["full name"] as? String
                            userToShow.imagePath = value["urlToImage"] as? String
                            userToShow.userID = value["uid"] as? String
                            userToShow.userLongitude = value["long"] as? String
                            userToShow.userLatitude = value["lat"] as? String

                            self.user.append(userToShow)
                        }


                    }

                }
            }
            DispatchQueue.main.async {
                self.map.reloadInputViews()

                //not sure if this is right
            }
        })

谢谢!!

【问题讨论】:

  • 嗨,Becca,您收到任何错误消息吗?你在地图上实际看到了什么?还有什么其他细节可以让我们了解您认为哪里出了问题?

标签: swift firebase annotations mapkit core-location


【解决方案1】:

这是一种预感,但我认为您正在追求这样的功能 - 您的努力已经非常接近了! NB - swift 语法中没有分号。

private func addUserAnnotation(user: User) {        

    let userAnnotation = MKPointAnnotation()
    let userLocation = CLLocationCoordinate2D(latitude: Double(user.userLatitude!), 
longitude: Double(user.userLongitude!))

    userAnnotation.coordinate = userLocation
    //userAnnotation.title = "Riders Location"
    self.map.addAnnotation(userAnnotation)

}

像这样调用函数 - 假设您想为用户数组中的第一个用户添加注释:

addUserAnnotation(user: user[0]) //addUserAnnotation(user[0]) also acceptable

这是用户的 OP 类。我觉得这也很重要

class User: NSObject {

    var userID: String!
    var fullName: String!
    var imagePath: String!
    var userLongitude: Double! // change from String!
    var userLatitude: Double!  // change from String!

}

【讨论】:

  • 我收到此错误消息“无法调用非函数类型 'MKPointAnnotation' 的值”Firebase 将所有数据保存为字符串...谢谢您的帮助!
  • 不用担心,让我们运行一个测试,看看是否有问题从用户属性 userLatitude 和 userLongitude 转换为 double。只需在其中添加一些十进制数字并查看是否将注释添加到您的地图中怎么样。您可以通过 addUserAnnotation 函数顶部的 print(user.userLatitude) 和 print(user.userLongitude) 找到合适的坐标。
  • 当我尝试打印用户坐标时,我什么也没得到。我也无法插入坐标并让它们工作......我将从那里开始
  • 好的,看来在检索坐标和显示注释方面都存在问题。如果您将代码移出 addUserAnnotation 并使用自指定坐标(不涉及用户类)直接进入 viewDidLoad() 会发生什么情况,这会绘制注释吗?
  • 我刚刚发现了一个不一致之处,即您在 User 类中将 userLongitude/latitude 声明为 String 但在上面的数据库检索中的 if let 语句中要求 Double ——这意味着以下代码在您创建的 userToShow 将不会被执行。我的建议是在您的 User 类中已经将 userLongitude 和 userLatitude 声明为 Double ,这样我们就不需要在检索到下游时处理该转换。
猜你喜欢
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
相关资源
最近更新 更多