所以我有一些坐标看起来像
N47° 15' 36.75",E011° 20' 38.28",+001906.00
我创建了一个类来解析并将它们转换为双精度
struct PLNWaypointCoordinate {
var latitude: Double = 0.0
var longitude: Double = 0.0
init(coordinateString: String) {
self.latitude = convertCoordinate(string: coordinateString.components(separatedBy: ",")[0])
self.longitude = convertCoordinate(string: coordinateString.components(separatedBy: ",")[1])
}
private func convertCoordinate(string: String) -> Double {
var separatedCoordinate = string.characters.split(separator: " ").map(String.init)
let direction = separatedCoordinate[0].components(separatedBy: CharacterSet.letters.inverted).first
let degrees = Double(separatedCoordinate[0].components(separatedBy: CharacterSet.decimalDigits.inverted)[1])
let minutes = Double(separatedCoordinate[1].components(separatedBy: CharacterSet.decimalDigits.inverted)[0])
let seconds = Double(separatedCoordinate[2].components(separatedBy: CharacterSet.decimalDigits.inverted)[0])
return convert(degrees: degrees!, minutes: minutes!, seconds: seconds!, direction: direction!)
}
private func convert(degrees: Double, minutes: Double, seconds: Double, direction: String) -> Double {
let sign = (direction == "W" || direction == "S") ? -1.0 : 1.0
return (degrees + (minutes + seconds/60.0)/60.0) * sign
}
}
我的问题是,正如标题所说,有没有更好、更安全的方法来执行这种转换?
我在这里学到的最后一种方法。抱歉,我找不到引用它的链接。