简体   繁体   English

将 CLLocationCoordinate2D(s) 的字符串转换为数组

[英]Convert String of CLLocationCoordinate2D(s) into array

Updating previous question for better explanation.更新先前的问题以获得更好的解释。

var breadcrumbs: [CLLocationCoordinate2D] = []
var path: [CLLocationCoordinate2D] = []

This is called every 10 seconds to append a CLLocationCoordinate2D to the array.每 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)
}

Once the user is done with their trip they tap a button and the following function is called.一旦用户完成他们的行程,他们点击一个按钮并调用以下函数。 This gets the data in to firebase.这会将数据输入到firebase。 I am saving as Strings as I get an error if done otherwise.我将保存为字符串,因为如果不这样做,我会收到错误。

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
        }
    }
}

In another screen I successfully pull out the String of Coordinates in a Firebase Database Reference.在另一个屏幕中,我成功提取了 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)]"

I want to be able to use this as an array from CLLocationCoordinate2D to draw a polyline using the following.我希望能够将其用作 CLLocationCoordinate2D 中的数组来使用以下内容绘制折线。 I am having trouble converting this string to a usable array of 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)
    }

If you have a suggestion on betting saving the original array, pulling it from Firebase, or converting the string please let me know.如果您对保存原始数组、从 Firebase 中提取或转换字符串有任何建议,请告诉我。 If you need other code for reference, please let me know.如果您需要其他代码供参考,请告诉我。

So I think your biggest problem is going to be that you are using String(describing:).所以我认为你最大的问题是你正在使用字符串(描述:)。 This is going to add those weird class names that you are seeing.这将添加您看到的那些奇怪的类名。 You are better off coming up with your own encoding method for lat/long and then reverse encoding it to get back your location data.您最好提出自己的纬度/经度编码方法,然后对其进行反向编码以取回您的位置数据。 So something like the following would be a bette approach.因此,类似以下的方法将是一种更好的方法。

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
}

I just used commas and colons for my encoding but you can easily use something else.我只是使用逗号和冒号进行编码,但您可以轻松地使用其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM