Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Swift – iOS – GPS – Coordinates – Lat/Long to DMS

Posted on June 8, 2018 By David Kittell

If you have a need for displaying the GPS location information in Degrees, minutes, and seconds (DMS) format these functions will help you out.

func latlon2DMS(latitude: Double) -> String {
        var latitudeSeconds = latitude * 3600
        let latitudeDegrees = latitudeSeconds / 3600
        latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)
        let latitudeMinutes = latitudeSeconds / 60
        latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)
        let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
        let latitudeDescription = String(format: "%.2f° %.2f' %.2f\" %@",
                                         abs(latitudeDegrees), abs(latitudeMinutes),
                                         abs(latitudeSeconds), latitudeCardinalDirection)
        return latitudeDescription
    }

    func latlon2DMS(longitude: Double) -> String {
        var longitudeSeconds = longitude * 3600
        let longitudeDegrees = longitudeSeconds / 3600
        longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)
        let longitudeMinutes = longitudeSeconds / 60
        longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)
        let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"
        let longitudeDescription = String(format: "%.2f° %.2f' %.2f\" %@",
                                          abs(longitudeDegrees), abs(longitudeMinutes),
                                          abs(longitudeSeconds), longitudeCardinalDirection)
        return longitudeDescription
    }
print(latlon2DMS(latitude: coordLat))
print(latlon2DMS(longitude: coordLong))
latitude.text = latlon2DMS(latitude: coordLat)
longitude.text = latlon2DMS(longitude: coordLong)

Reference: https://stackoverflow.com/questions/27996351/swift-convert-decimal-coordinate-into-degrees-minutes-seconds-direction/50765689#50765689

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code iOS Swift

Post navigation

Previous post
Next post

Related Posts

UNIX – List USB Devices With lsusb

Posted on February 12, 2018February 12, 2018

On the Mac “lsusb -v” is rather clean in comparison to other systems. # If not already installed… # brew update && brew tap jlhonora/lsusb && brew install lsusb lsusb -v lsusb -v # or lsusb -v | grep -E ‘\<(Bus|iProduct|bDeviceClass|bDeviceProtocol|idVendor|idProduct|iManufacturer)’ 2>/dev/null All information on this site is shared with…

Read More

Visual Studio Oracle Tools

Posted on February 28, 2013October 26, 2015

When working with Visual Studio and Oracle you will need to download Oracle’s Visual Studio Tools. Code Samples: http://www.oracle.com/technetwork/articles/dotnet/cook-dotnet-101788.html If I thought I could legally host the file I would. Official Page: http://www.oracle.com/technetwork/topics/dotnet/utilsoft-086879.html If the link fails to work do a search on http://www.oracle.com/ for Visual Studio ODAC 11.2 Release…

Read More

Mac OSX Terminal – Get System Details

Posted on March 30, 2016

If you only have SSH access to a Mac OSX server and need some details on the machine you can use these commands to help. uname -a Darwin MyMacMini 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64 sw_vers ProductName: Mac OS X ProductVersion: 10.11.3 BuildVersion:…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
©2025 David Kittell | WordPress Theme by SuperbThemes