【问题标题】:Representing Coordinates from Firebase as Annotations Swift将 Firebase 中的坐标表示为注释 Swift
【发布时间】:2017-11-08 22:17:57
【问题描述】:

我无法从上传到 Firebase 的纵向和纬度角度在地图上添加注释。我想为我的所有用户添加注释,但无法让 xcode 在另一个函数中识别我的 userLatitude 和 userLongitude 变量。

任何事情都会有帮助!

    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 imagePath = value["urlToImage"] as? String,
                        //String(format: "%f", self.currentUserLocation?.longitude ?? 0),
                        let userLongitude = value["long"] as? CLLocationDegrees,
                        let userLatitude = value["lat"] as? CLLocationDegrees
                        //why isnt it recognizing the users degrees

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

                            self.user.append(userToShow)
                        }


                    }

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

                //not sure if this is right
            }
        })

    }




    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {

        let otherUserLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(userLatitude, userLongitude)

        let userAnnotation = MKPointAnnotation()
        userAnnotation.coordinate = otherUserLocation
        userAnnotation.title = "fullName"





    }

【问题讨论】:

    标签: swift firebase annotations mapkit core-location


    【解决方案1】:

    如果您将 as? CLLocationDegrees 更改为 as? String 并在之后管理 CLLocationDegrees 中的演员表?

    因为我认为,在 Firebase 中,数据存储为 String 而不是 Double(这是 CLLocationDegrees 类型所指的数据类型)。

    【讨论】:

    • 将字符串更改为双精度的格式是什么?我现在遇到了麻烦。
    • 通常情况下,一个简单的 Double(longitude) 和 Double(latitude) 应该可以完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多