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

Swift 如何以相同的顺序检索坐标?,swift,firebase,mapkit,Swift,Firebase,Mapkit,这是用于保存多段线的JSON结构,我需要以相同的顺序检索坐标 这是我获取坐标和创建多段线的代码 "-KHbCuQflKHrJiUmfpG0" : { "coordinates" : { "-KHbCuQflKHrJiUmfpG1" : { "latitude" : 13.17078652595298, "longitude" : -59.5775944578738 }, "-KHbCuQflKHrJiUmfpG2" : { "lat

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

这是我获取坐标和创建多段线的代码

"-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"
},
//geoobject.key-是geoobject的ID(用于循环geoobjects)“-KHbCuQflKHrJiUmfpG0”
DataService.DataService.getGeoObject(geoobject.key).observeEventType(.Value,withBlock:{geoSnapshot in
var CoordinatePolyline=[CLLocationCoordinate2D]()
如果让geoDictionary=geoSnapshot.value作为字典{
让id=geoSnapshot.key
如果地理索引[“类型”]为!字符串==“多段线”{
如果让coords=geoDictionary[“坐标”]作为?[String:[String:Double]]{
让contents=coords.values
内容中的内容{
让纬度=内容[“纬度”]
让经度=内容[“经度”]
let point=CLLocationCoordinate2D(纬度:纬度!经度:经度!)
坐标多边形追加(点)
}
//coordintesPolyline包含随机移动坐标
设多段线=多段线(坐标:&coordinatesPolyline,计数:coordinatesPolyline.count)
//坐标=[]
polyline.id=geoSnapshot.key
polyline.title=地理索引[“title”]作为字符串
polyline.subtitle=地理索引[“subtitle”]作为?字符串
self.polylines.insert(多段线,索引:0)
}
}
})
我渲染了多段线,但多段线的渲染顺序不好,因为坐标是无序的

有人知道如何以正确的顺序获取坐标吗?Firebase检索坐标时会进行无序处理,然后渲染效果不好


谢谢您的建议。

按.Value读取firebase数据时,节点内的所有数据都将在快照中返回

如果您希望数据按特定顺序排列,则需要确定排序依据的“对象”。自然顺序是按键排序,但如果您希望数据按其他参数排序,则需要将其包含在firebase结构中,然后将orderedBy添加到您的表中

如果您拥有data by.Value,则需要迭代快照中的每个子节点以访问该节点数据

如果您的Firbase结构是

// 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)
                                }
                        }
                    })
然后快照包含所有这些数据。迭代快照并处理每个子项:

"coordinates" : {
    "-KHbCuQflKHrJiUmfpG1" : {
      "latitude" : 13.17078652595298,
      "longitude" : -59.5775944578738
    },
    "-KHbCuQflKHrJiUmfpG2" : {
      "latitude" : 13.15541190861343,
      "longitude" : -59.57619643155932
    },
    "-KHbCuQg9W_tebl1pU66" : {
      "latitude" : 13.148444967591,
      "longitude" : -59.5589266947333
    }
(未测试该代码,但已接近)

Oh,还有一件事。ObserveEventOfType(.Value)将持续观察节点的更改-添加、更改或删除,并在这些事件发生时随时触发


如果您只需要一次快照,请使用ObserveSingleEventOfType(.Value)

按.Value读取firebase数据时,将在快照中返回节点内的所有数据

如果您希望数据按特定顺序排列,则需要确定排序依据的“对象”。自然顺序是按键排序,但如果您希望数据按其他参数排序,则需要将其包含在firebase结构中,然后将orderedBy添加到您的表中

如果您拥有data by.Value,则需要迭代快照中的每个子节点以访问该节点数据

如果您的Firbase结构是

// 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)
                                }
                        }
                    })
然后快照包含所有这些数据。迭代快照并处理每个子项:

"coordinates" : {
    "-KHbCuQflKHrJiUmfpG1" : {
      "latitude" : 13.17078652595298,
      "longitude" : -59.5775944578738
    },
    "-KHbCuQflKHrJiUmfpG2" : {
      "latitude" : 13.15541190861343,
      "longitude" : -59.57619643155932
    },
    "-KHbCuQg9W_tebl1pU66" : {
      "latitude" : 13.148444967591,
      "longitude" : -59.5589266947333
    }
(未测试该代码,但已接近)

Oh,还有一件事。ObserveEventOfType(.Value)将持续观察节点的更改-添加、更改或删除,并在这些事件发生时随时触发


如果只需要一次拍摄,请使用ObserveSingleEventOfType(.Value)

为什么您的JSON不包含数组?这是个大问题吗?我创建了这个字典结构。字典没有顺序,所以您需要连续解析JSON才能知道顺序……请给我一些建议,我不能用数组更改保存坐标。@Wain No-Firebase在服务器级别处理查询参数,并进行排序和排序nds数据基于查询的构造方式。数组无法查询(嗯,有点)单个元素无法更新,整个节点必须重新写入。一般来说,Firebase结构的路由比数组更好。为什么JSON不包含数组?这是个大问题吗?我创建了这个字典结构。字典没有顺序,所以你需要连续解析JSON才能知道顺序…请告诉我需要一些建议,我不能用数组更改保存坐标。@Wain No-Firebase在服务器级别处理查询参数,并根据查询的构造方式对数据进行排序和发送。数组无法查询(嗯,有点)单个元素无法更新,整个节点必须重新写入。一般来说,Firebase结构的路由比数组更好。