I have a value like following value. I want to convert these value into longitude and latitude coordinates. If possible with math operations.
我有如下值。我想把这些值转换成经度和纬度坐标。如果可能的话,用数学运算。
Input:
输入:
latitude: 40,58,36, N
longitude: 28,49, 16, E
Output:
输出:
latitude: 40.976667
longitude: 28.821111
2 个解决方案
#1
2
I don't know if there is a built-in function, but this will work:
我不知道是否有内置的功能,但这将会起作用:
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
#2
0
you can extend your String class: Swift3 example:
可以扩展字符串类: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
}
Simply call this function on your Coordinates String
只需在坐标字符串上调用这个函数
let myLatitude = "40,58,36,N".convertCoordinatesToDouble()
#1
2
I don't know if there is a built-in function, but this will work:
我不知道是否有内置的功能,但这将会起作用:
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
#2
0
you can extend your String class: Swift3 example:
可以扩展字符串类: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
}
Simply call this function on your Coordinates String
只需在坐标字符串上调用这个函数
let myLatitude = "40,58,36,N".convertCoordinatesToDouble()