"-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檢索座標shuffle,然後渲染效果不佳。
謝謝你的一些建議。
爲什麼沒有你的JSON包含一個數組? – Wain
是大問題嗎?我創建了這個字典結構。 – sparky
字典沒有訂單,因此您需要連續解析JSON以瞭解訂單... – Wain