How to convert CLLocation to string in Xcode

不想你离开。 提交于 2019-12-30 07:33:08

问题


I use SwiftLocation for get coordinates:

try! SwiftLocation.shared.currentLocation(Accuracy.House, timeout: 20, onSuccess: { (location) -> Void in
    print(location?.description)            
         }) { (error) -> Void in
    print("something went wrong")
         }

In location?.description I get it:

<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2/22/16, 6:35:55 PM Indochina Time

And I need to take just coordinates. So I make loop for it:

    while name.characters.last != ">" { // delete all after ">"
        name = String(name.characters.dropLast())
    }
    name = String(name.characters.dropLast()) // delete ">"
    name = String(name.characters.dropFirst()) // delete "<"
    name = String(name.characters.dropFirst()) // delete "+"
    print(name) // get 37.78583400,-122.40641700
    tempMyLat = name
    while tempMyLat.characters.last != "," { // delete all after ","
        tempMyLat = String(tempMyLat.characters.dropLast())
    }
    tempMyLon = name
    while tempMyLon.characters.first != "," { // delete all before ","
        tempMyLon = String(tempMyLon.characters.dropFirst())
    }
    tempMyLon = String(tempMyLon.characters.dropFirst()) // delete ","

But this code work ONLY for string. When I get location?.description - I can't convert it for type - string, because "location" is a CLLocation type.

So, my question: How convert location?.description to String ? or how get only coordinates from the SwiftLocation

Thanks.


回答1:


If you want to get locations from CLLocation you dont need to convert string from CLLocation object. you can get locations directly from CLLocation object. Here is an example :

var locationObj = locationArray.lastObject as CLLocation
var coord = locationObj.coordinate
var longitude = coord.longitude //Latitude & Longitude as String
var latitude = coord.latitude



回答2:


In Xcode 10+ and Swift 3, you may have to do something similar to the following:

let myLocation: CLLocation = locations[0] as CLLocations
var myLatitude: String = String(format: "%f", myLocation.coordinate.latitude)
var myLongitude: String = String(format:"%f", myLocation.coordinate.longitude)



回答3:


I'd create an extension for CLLocation like this:

extension CLLocation {
    /// Provide optional coordinate components labels
    func locationString(with labels:[String]? = ["lat","lon"]) -> String {
        return "\(latitudeString(with: labels!.first!))- \(longitudeString(with: labels!.last!))"
    }

    // Get string for each component
    //This is not necessary, as you could combine this into formattedLabel: label 
    //But it's good to have these separate in case you need just one of the components
    func latitudeString(with label: String?) -> String {
        return "\(formattedLabel(from: label))\(self.coordinate.latitude)"
    }

    func longitudeString(with label: String?) -> String {
        return "\(formattedLabel(from: label))\(self.coordinate.longitude)"
    }

    // Returns formatted label or nothing in case nothing is needed
    func formattedLabel(from label: String?) -> String {
        var sortedLabel = ""
        if label != nil {
            sortedLabel = "\(label!): "
        }
        return sortedLabel
    }
}

And then call it like:

let locationString = someCLLocation.locationString()
// or someCLLocation.locationString(with: ["label 1", "label 2"])


来源:https://stackoverflow.com/questions/35569835/how-to-convert-cllocation-to-string-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!