我有一个像值一样的值。我想将这些值转换为经度和纬度坐标。如果可能,可以使用数学运算。
输入:
latitude: 40,58,36, N
longitude: 28,49, 16, E
输出:
latitude: 40.976667
longitude: 28.821111
答案 0 :(得分:2)
我不知道是否有内置函数,但这可行:
func convert(degrees 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
}
let lattitude = convert(degrees: 40, minutes: 58, seconds: 36, direction: "N") // 40.9766666666667
let longitude = convert(degrees: 28, minutes: 49, seconds: 16, direction: "W") // -28.8211111111111
答案 1 :(得分:0)
你可以扩展你的String类: Swift3示例:
extension String {
func convertCoordinatesToInteger() -> Double {
let array = self.components(separatedBy: ",")
let sign = (array[2] == "W" || array[2] == "S") ? -1.0 : 1.0
let deg = Double(array[0])!
let min = Double(array[1])!
let sec = Double(array[2])!
return (deg + (min + sec/60.0)/60.0) * sign
}
只需在您的坐标字符串上调用此函数
let myLatitude = "40,58,36,N".convertCoordinatesToDouble()