gpt4 book ai didi

swift - 通过 Firebase Swift 显示和更新注释

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:58 25 4
gpt4 key购买 nike

我有以下 Firebase 数据库结构:

{
"Users" : {
"TzDyIOXukcVYFr8HEBC5Y9KeOyJ2" : {
"Contact Number" : "",
"Name" : "",
"User Email" : "test@test.com"
},
"xqKdv6hN1wSGplvKh3sYuutFmwz2" : {
"Contact Number" : "",
"Name" : "",
"User Email" : “test2@test.com”
}
},
"buildings-list" : {
"-KTd0ZU-pJHYBs282RfI" : {
"addedByUser" : "TzDyIOXukcVYFr8HEBC5Y9KeOyJ2",
"content" : "Post 1",
"cost" : "5000",
"duration" : "Annually",
"latitude" : "25.2003331668539",
"longitude" : "55.2563433525882",
"number" : "1234567890",
"timestamp" : "Sun 09 Oct"
}
}
}

我有一个对 Firebase 数据库的 .observe 引用,它根据纬度和经度向 map 添加标记。

func displayAllMarkers() {

dbRef.observe(.childAdded, with: { snapshot in

let latitude = (snapshot.value as AnyObject!)!["latitude"] as! String!
let latitudeDouble = Double(latitude!)
let longitude = (snapshot.value as AnyObject!)!["longitude"] as! String!
let longitudeDouble = Double(longitude!)
let markerTitle = (snapshot.value as AnyObject!)!["content"] as! String!
let durationTitle = (snapshot.value as AnyObject!)!["duration"] as! String!
let costTitle = (snapshot.value as AnyObject!)!["cost"] as! String!
let timestamp = (snapshot.value as AnyObject!)!["timestamp"] as! String!
self.telephoneNumber = (snapshot.value as AnyObject!)!["number"] as! String!

let annotation = myAnnotationView(title: markerTitle!.capitalized, coordinate: CLLocationCoordinate2D(latitude: latitudeDouble!, longitude: longitudeDouble!), duration: durationTitle!, cost: costTitle!, info: "", timestamp: timestamp!, contactNumber: self.telephoneNumber!)
//my annotationView is my custom class for annotations.
self.mapView.addAnnotation(annotation as MKAnnotation)


})}

这成功地将标记添加到 map 中,但是当删除子项时,标记不会更新/从 map 中删除。我需要添加 for 循环吗?...感谢任何帮助。

更新:我已经使用我现有的结构最小化了代码。

func getMarkers() {

dbRef.observe(.value, with: { (snapshot) in

for buildings in snapshot.children {

let buildingsObject = Buildings (snapshot: buildings as! FIRDataSnapshot)
print(buildingsObject)

let latDouble = Double(buildingsObject.latitude!)
let lonDouble = Double(buildingsObject.longitude!)

let annotation = myAnnotationView.init(title: buildingsObject.content, coordinate: CLLocationCoordinate2D.init(latitude: latDouble!, longitude: lonDouble!), duration: buildingsObject.duration, cost: buildingsObject.cost, info: "", timestamp: buildingsObject.timestamp, contactNumber: buildingsObject.number)

print(snapshot)

self.mapView.addAnnotation(annotation as MKAnnotation)

}})}

最佳答案

.childAdded 上的 CMD+CLICK,您将被定向到 FIRDataEventType 类文档。

case childAdded // 0, fired when a new child node is added to a location

case childRemoved // 1, fired when a child node is removed from a location

case childChanged // 2, fired when a child node at a location changes

case childMoved // 3, fired when a child node moves relative to the other child nodes at a location

case value // 4, fired when any data changes at a location and, recursively, any children

您的选择:-

  • 因此,如果您想监听 child 是否被删除的位置,您的 eventType 应该是 .childRemoved

  • 但如果您想避免为 .childRemoved 加载另一个线程(这可能会增加您应用的内存消耗),只需使用 。 value 而不是 eventType 的任何一个。但是整个快照都会被加载,所以不要忘记遍历所有快照,然后添加注释。

  • 使用 .value 会增加有限的带宽消耗。因此,更好的方法是创建一个结构并将注释添加到该类型结构的数组中。并显示该数组中的注释。每当添加或删除注释时,相应地对您的数组和 firebase 数据库执行相同的操作。

关于swift - 通过 Firebase Swift 显示和更新注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com