Reputation: 577
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.
Upvotes: 2
Views: 6796
Reputation: 1176
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"])
Upvotes: 2
Reputation: 437
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)
Upvotes: 3
Reputation: 481
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
Upvotes: 2