2016-05-13 68 views
1
"-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,然後渲染效果不佳。

謝謝你的一些建議。

+0

爲什麼沒有你的JSON包含一個數組? – Wain

+0

是大問題嗎?我創建了這個字典結構。 – sparky

+1

字典沒有訂單,因此您需要連續解析JSON以瞭解訂單... – Wain

回答

0

當通過.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 
    } 

然後快照包含了所有的數據。遍歷快照,並與每一個孩子的工作:

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的)