swift - 从 Firebase 获取坐标以进行注释

标签 swift firebase annotations mapkit core-location

我目前正在尝试从 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
            }
        })

谢谢!!

最佳答案

这是一种预感,但我认为您正在寻找这样的功能 - 您已经非常接近您的努力了!注意 - 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!

}

关于swift - 从 Firebase 获取坐标以进行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193548/

相关文章:

swift - 通过按下第二个接口(interface) Controller 中的按钮来增加第一个接口(interface) Controller 中的计数器

ios - 如何在 swift 中找到 QuadCurve 和 Line UIBezierPaths 之间的交点(CGPoint)?

ios - gRPCCertificates.bundle : No such file or directory

javascript - 使用 Rxjs 过滤 Observable

java - Lombok @Singular 在构造函数上使用 @Builder 的子类中没有效果

swift - 如何将标签设置为 Float 类型,以便我可以让标签描绘 slider 值(Xcode Swift)

java - 如何从多个 firebase 数据库中读取数据?

android - Activity_ 不是抽象的,不会覆盖 HasViews 中的抽象方法 findViewById(int)

java - 如何从另一个类获取调用者方法的注释

iOS 11 UITableView performBatchUpdates 完成关闭未被调用