ardakaraca ardakaraca - 2 months ago 17
Swift Question

How to convert degrees, minutes, seconds and direction to longitude and latitude coordinates?

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

Answer

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
Comments