将CLLocationCoordinate2D的字符串转换为数组

Eri*_*.18 1 cllocation ios firebase swift firebase-realtime-database

更新先前的问题以获得更好的解释。

var breadcrumbs: [CLLocationCoordinate2D] = []
var path: [CLLocationCoordinate2D] = []
Run Code Online (Sandbox Code Playgroud)

每10秒钟调用一次,以将CLLocationCoordinate2D附加到数组。

func addBreadcrumb(){
    let speed = (locationmanager.location?.speed)!
    let speedRounded = speed.roundTo(places: 4)
    let crumbCoordinate = locationmanager.location?.coordinate

    breadcrumbs.append(crumbCoordinate!)
    tripSpeeds.append(speedRounded)
}
Run Code Online (Sandbox Code Playgroud)

一旦用户完成行程,他们便会点击一个按钮,并调用以下功能。这会将数据输入到Firebase中。我将另存为字符串,否则将收到错误消息。

func submitTripPath(){
    let tripID: String? = tripUID
    let tripPath = String(describing: breadcrumbs) //This may be the problem
    let speeds = String(describing: tripSpeeds)

    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    let tripRef = ref.child("TripPaths").child(tripID!)
    let tripDictionary = ["routePath" : tripPath, "routeSpeed" : speeds] as [String : Any]
    tripRef.updateChildValues(tripDictionary) { (err, ref) in
        if err != nil {
            print(err!)
            return
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在另一个屏幕中,我成功地在Firebase数据库参考中提取了坐标字符串。

let routePath = dict["routePath"] as! String //This may also be an issue
//output looks like this
"[__C.CLLocationCoordinate2D(latitude: 37.337728550000001, longitude: -122.02796406), __C.CLLocationCoordinate2D(latitude: 37.337716899999997, longitude: -122.02835139), __C.CLLocationCoordinate2D(latitude: 37.337694319999997, longitude: -122.0287719)]"
Run Code Online (Sandbox Code Playgroud)

我希望能够将其用作CLLocationCoordinate2D中的数组以使用以下内容绘制折线。我在将此字符串转换为可用的CLLocationCoordinate2D数组时遇到麻烦。

    if (path.count > 1) {
        let sourceIndex = path.count - 1
        let destinationIndex = path.count - 2

        let c1 = path[sourceIndex]
        let c2 = path[destinationIndex]

        var a = [c1, c2]
        let polyline = MKPolyline(coordinates: &a, count: a.count)
        mapContainerView.add(polyline)
    }
Run Code Online (Sandbox Code Playgroud)

如果您有关于下注保存原始数组,从Firebase提取数组或转换字符串的建议,请告诉我。如果您需要其他代码作为参考,请告诉我。

Bin*_*pus 5

所以我认为您最大的问题将是您正在使用String(describing :)。这将添加您看到的那些奇怪的类名称。您最好提出自己的经/纬度编码方法,然后对其进行反向编码以获取位置数据。因此,类似以下内容的方法将是一种bette方法。

func submitTripPath(){
    let tripID: String? = tripUID
    let tripPath = encodeCoordinates(coords: breadcrumbs)
    let speeds = String(describing: tripSpeeds)

    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()
    let tripRef = ref.child("TripPaths").child(tripID!)
    let tripDictionary = ["routePath" : tripPath, "routeSpeed" : speeds] as [String : Any]
    tripRef.updateChildValues(tripDictionary) { (err, ref) in
        if err != nil {
            print(err!)
            return
        }
    }
}

func encodeCoordinates(coords: [CLLocationCoordinate2D]) -> String {
    let flattenedCoords: [String] = coords.map { coord -> String in "\(coord.latitude):\(coord.longitude)" }
    let encodedString: String = flattenedCoords.joined(separator: ",")
    return encodedString
}

func decodeCoordinates(encodedString: String) -> [CLLocationCoordinate2D] {
    let flattenedCoords: [String] = encodedString.components(separatedBy: ",")
    let coords: [CLLocationCoordinate2D] = flattenedCoords.map { coord -> CLLocationCoordinate2D in
        let split = coord.components(separatedBy: ":")
        if split.count == 2 {
            let latitude: Double = Double(split[0]) ?? 0
            let longitude: Double = Double(split[1]) ?? 0
            return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        } else {
            return CLLocationCoordinate2D()
        }
    }
    return coords
}
Run Code Online (Sandbox Code Playgroud)

我只是使用逗号和冒号作为编码,但是您可以轻松使用其他名称。