Convert decimal coordinate into degrees, minutes, seconds, direction

前端 未结 6 1727
遥遥无期
遥遥无期 2020-12-28 21:13

I have the following so far, but can\'t figure out a tidy way to get the direction letters in without a bunch of messy if statements. Any ideas? Ideally I\'d like to extend

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 21:39

    Regarding to Alex's answer, here is a solution in Swift 3 with an output tuple. It returns the coordinates in a tuple.

    Furthermore, it really extends the class CLLocationDegrees and doesn't require an extra parameter.

    import MapKit
    
    extension CLLocationDegrees {
    
        func degreeRepresentation() -> (northOrEast: Bool, degrees: Int, minutes: Int, seconds: Int) {
            var inputSeconds = Int(self * 3600)
            let inputDegrees = inputSeconds / 3600
            inputSeconds = abs(inputSeconds % 3600)
            let inputMinutes = inputSeconds / 60
            inputSeconds %= 60
    
            return (inputDegrees > 0, abs(inputDegrees), inputMinutes, inputSeconds)
        }
    
    }
    

提交回复
热议问题