从Firebase检索用户纬度和经度后添加注释

时间:2016-09-27 20:37:49

标签: ios swift firebase firebase-realtime-database mapkit

我正在尝试从Firebase检索其纬度和经度后添加所有在线用户的mapView注释。我可以打印它作为可选项或作为CLLocationDegrees又名Double,但当我尝试将其添加到我的user.userAnnotation属性时,我得到一个致命的错误。这就是我正在打印的内容:

this is the users lat Optional(19.435477800000001) 
this is the user latitude in CLLocationDegrees 19.4354778 
fatal error: unexpectedly found nil while unwrapping an Optional value

这是我的职责:

func fetchOnlineUsers() {
    ref = FIRDatabase.database().reference()

    FIRDatabase.database().reference().child("users").observe(.childAdded, with: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = AppUser()
            user.userEmail = (snapshot.value as? NSDictionary)?["name"] as? String
            user.latitude = (snapshot.value as? NSDictionary)?["latitude"] as? Double
            user.longitude = (snapshot.value as? NSDictionary)?["longitude"] as? Double
            user.online = ((snapshot.value as? NSDictionary)?["online"] as? Bool)!
            print("this is the user latitude \(user.latitude)")
            let userLat = CLLocationDegrees(user.latitude!)
            let userLon = CLLocationDegrees(user.longitude!)  

            if user.online == true {
                user.userAnnotation.coordinate = CLLocationCoordinate2D(latitude: userLat, longitude: userLon)

                self.users.append(user)
            }
            print("this are the users \(self.users)")
            print("this is the dictionary \(dictionary)")

            self.mainMapView.addAnnotations([user.userAnnotation])
        }
    }, withCancel: nil)
}

1 个答案:

答案 0 :(得分:1)

你得到的一些价值是fetch.get();。试试这个:

 function data_user($dbc,$email){

  $q = "SELECT * FROM doctors WHERE email= ?";
  $stmt = mysqli_prepare($dbc, $q);
  mysqli_stmt_bind_param($stmt, "s", $email);
  mysqli_stmt_execute($stmt);
  $r = mysqli_stmt_get_result($stmt);
  return $r
}

如果成功,您可以开始使用$r = data_user($dbc,$email); while($info = mysqli_fetch_assoc($r){ echo $info['user_name']; } 中创建的变量,它们将是1:有值,2:不是可选的。