gpt4 book ai didi

swift - 如何以相同的顺序检索坐标?

转载 作者:行者123 更新时间:2023-11-28 08:38:01 25 4
gpt4 key购买 nike

"-KHbCuQflKHrJiUmfpG0" : {
"coordinates" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}
},
"subtitle" : "patrick",
"title" : "River",
"type" : "polyline"
},

这是我用于保存折线的 JSON 结构,我需要以相同的顺序检索坐标。

这是我获取坐标和创建折线的代码。

// geoobject.key - is ID of geoobject (for loop geoobjects) "-KHbCuQflKHrJiUmfpG0" 
DataService.dataService.getGeoObject(geoobject.key).observeEventType(.Value, withBlock: { geoSnapshot in
var coordinatesPolyline = [CLLocationCoordinate2D]()
if let geoDictionary = geoSnapshot.value as? Dictionary<String, AnyObject> {
let id = geoSnapshot.key
if geoDictionary["type"] as! String == "polyline" {
if let coords = geoDictionary["coordinates"] as? [String:[String:Double]] {
let contents = coords.values
for content in contents {
let latitude = content["latitude"]
let longitude = content["longitude"]
let point = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
coordinatesPolyline.append(point)
}
// coordintesPolyline contains shuffle coordinates
let polyline = Polyline(coordinates: &coordinatesPolyline, count: coordinatesPolyline.count)
//coordinates = []
polyline.id = geoSnapshot.key
polyline.title = geoDictionary["title"] as? String
polyline.subtitle = geoDictionary["subtitle"] as? String
self.polylines.insert(polyline, atIndex: 0)
}
}
})

我渲染了多段线,但多段线渲染不好,因为坐标是乱序的。

有谁知道如何以正确的顺序获取坐标。 Firebase 检索坐标随机播放,然后渲染效果不佳。

谢谢你的一些建议。

最佳答案

当通过 .Value 读取 firebase 数据时,节点内的所有数据都会在快照中返回。

如果您想要特定顺序的数据,您需要决定您希望它按什么“事物”排序。自然顺序是按键,但如果您希望它按其他参数排序,则需要将其包含在您的 firebase 结构中,然后将 orderedBy 添加到您的观察中。

您通过 .Value 获得数据,然后您需要遍历快照中的每个子节点以访问该节点数据。例如

如果你的 Firbase 结构是

"coordinates" : {
"-KHbCuQflKHrJiUmfpG1" : {
"latitude" : 13.17078652595298,
"longitude" : -59.5775944578738
},
"-KHbCuQflKHrJiUmfpG2" : {
"latitude" : 13.15541190861343,
"longitude" : -59.57619643155932
},
"-KHbCuQg9W_tebl1pU66" : {
"latitude" : 13.148444967591,
"longitude" : -59.5589266947333
}

然后快照包含所有这些数据。遍历快照并与每个 child 一起工作:

ref.observeEventType(.Value, withBlock: { geoSnapshot in

for child in geoSnapshot.children {
let lat = child["latitude"] as? String
let long = child["longitude"] as? String

print("lat = \(lat) long = \(long)"
}
})

(没有测试该代码,但它很接近)

哦,还有一件事。 ObserveEventOfType(.Value) 将持续观察节点的变化 - 添加、更改或删除,并会在其中一个事件发生时触发。

如果您只想要一次性,请使用 ObserveSingleEventOfType(.Value)

关于swift - 如何以相同的顺序检索坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37208784/

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