I am able to determine the latitude and longitude of a location in the center of a map with the following:
func TargetGridReference(outletMapView_PrimaryTargetLocationMap: MKMapView, regionDidChangeAnimated animated: Bool) {
let ThreatGridReference = outletMapView_PrimaryTargetLocationMap.centerCoordinate
let mapLatitude = String(format:"%.13f",ThreatGridReference.latitude)
let mapLongitude = String(format:"%.13f",ThreatGridReference.longitude)
let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"
self.outletLabel_TargetGridReference.text = latAndLong
tempThreatGridReference = ThreatGridReference
tempThreatGridReferenceLat = mapLatitude
tempThreatGridReferenceLon = mapLongitude
}
This gives me the latitude for the Eiffel Tower in Paris as:
Lat: 48.8582487759147
Long: 2.2945180844931
How can I convert the above to the following DMS format in Swift?:
lat: 48° 51' 29.6956" N
long: 2° 17' 40.2651" E
Does MapKit provide automatic conversion into the various coordinate systems like UTM, UPS, MGRS, etc?
Unfortunately there is no automatic conversion but you can easily create one as follow:
Swift 4 or later
func coordinateToDMS(latitude: Double, longitude: Double) -> (latitude: String, longitude: String) {
let latDegrees = abs(Int(latitude))
let latMinutes = abs(Int((latitude * 3600).truncatingRemainder(dividingBy: 3600) / 60))
let latSeconds = Double(abs((latitude * 3600).truncatingRemainder(dividingBy: 3600).truncatingRemainder(dividingBy: 60)))
let lonDegrees = abs(Int(longitude))
let lonMinutes = abs(Int((longitude * 3600).truncatingRemainder(dividingBy: 3600) / 60))
let lonSeconds = Double(abs((longitude * 3600).truncatingRemainder(dividingBy: 3600).truncatingRemainder(dividingBy: 60) ))
return (String(format:"%d° %d' %.4f\" %@", latDegrees, latMinutes, latSeconds, latitude >= 0 ? "N" : "S"),
String(format:"%d° %d' %.4f\" %@", lonDegrees, lonMinutes, lonSeconds, longitude >= 0 ? "E" : "W"))
}
let dms = coordinateToDMS(latitude: 48.8582487759147, longitude: 2.2945180844931)
print(dms.latitude) // "48° 51' 29.6956" N"
print(dms.longitude) // "2° 17' 40.2651" E"
You can also extend CLLocationCoordinate2D to get your latitude/longitude DMS description:
import MapKit
extension FloatingPoint {
var minutes: Self {
return (self*3600)
.truncatingRemainder(dividingBy: 3600)/60
}
var seconds: Self {
return (self*3600)
.truncatingRemainder(dividingBy: 3600)
.truncatingRemainder(dividingBy: 60)
}
}
extension CLLocationCoordinate2D {
var dms: (latitude: String, longitude: String) {
return (String(format:"%d° %d' %.4f\" %@",
Int(abs(latitude)),
Int(abs(latitude.minutes)),
abs(latitude.seconds),
latitude >= 0 ? "N" : "S"),
String(format:"%d° %d' %.4f\" %@",
Int(abs(longitude)),
Int(abs(longitude.minutes)),
abs(longitude.seconds),
longitude >= 0 ? "E" : "W"))
}
}
let coord = CLLocationCoordinate2D(latitude: 48.8582487759147, longitude: 2.2945180844931)
coord.dms.latitude // "48° 51' 29.6956" N"
coord.dms.longitude // "2° 17' 40.2651" E"
I made some updates to this code to get it to work in Swift3 (mostly using .truncatingRemainder(dividingBy: )
rather than %
) and also expanded it to output DMM as well, and included functions for converting DMM and DMS to DD!
extension CLLocationCoordinate2D {
var latitudeMinutes: Double { return (latitude * 3600).truncatingRemainder(dividingBy: 3600) / 60 }
var latitudeSeconds: Double { return ((latitude * 3600).truncatingRemainder(dividingBy: 3600)).truncatingRemainder(dividingBy: 60) }
var longitudeMinutes: Double { return (longitude * 3600).truncatingRemainder(dividingBy: 3600) / 60 }
var longitudeSeconds: Double { return ((longitude * 3600).truncatingRemainder(dividingBy: 3600)).truncatingRemainder(dividingBy: 60) }
var dms:(latitude: String, longitude: String) {
return (String(format:"%d°%d'%.1f\"%@",
Int(abs(latitude)),
Int(abs(latitudeMinutes)),
abs(latitudeSeconds),
latitude >= 0 ? "N" : "S"),
String(format:"%d°%d'%.1f\"%@",
Int(abs(longitude)),
Int(abs(longitudeMinutes)),
abs(longitudeSeconds),
longitude >= 0 ? "E" : "W"))
}
var dmm: (latitude: String, longitude: String) {
return (String(format:"%d°%.4f'%@",
Int(abs(latitude)),
abs(latitudeMinutes),
latitude >= 0 ? "N" : "S"),
String(format:"%d°%.4f'%@",
Int(abs(longitude)),
abs(longitudeMinutes),
longitude >= 0 ? "E" : "W"))
}
}
var coord = CLLocationCoordinate2D(latitude: 41.40338, longitude: 2.17403)
coord.latitude // 41.40338
coord.longitude // 2.17403
coord.latitudeMinutes // 24.20280000000009
coord.latitudeSeconds // 12.16800000000512
coord.longitudeMinutes // 10.44180000000001
coord.longitudeSeconds // 26.50800000000072
coord.dms.latitude // "41°24'12.2"N"
coord.dms.longitude // "2°10'26.5"E"
coord.dmm.latitude // "41°24.2028'N"
coord.dmm.longitude // "2°10.4418'E"
These conversion functions also allow an input without indicating cardinal direction, but rather just supplying positive or negative:
func DMStoDD(latDeg: Double, latMin: Double, latSec: Double, latDir: String?, longDeg: Double, longMin: Double, longSec: Double, longDir: String?) -> CLLocationCoordinate2D {
var latitude = CLLocationDegrees()
if latDeg > 0 {
latitude = CLLocationDegrees(latDeg + ((latMin*60)/3600) + (latSec/3600))
if latDir == "S" {latitude *= -1}
}
else{
latitude = CLLocationDegrees((latDeg * -1) + ((latMin*60)/3600) + (latSec/3600))
latitude *= -1
}
var longitude = CLLocationDegrees()
if longDeg > 0 {
longitude = CLLocationDegrees(longDeg + ((longMin*60)/3600) + (longSec/3600))
if longDir == "W" {longitude *= -1}
}
else{
longitude = CLLocationDegrees((longDeg * -1) + ((longMin*60)/3600) + (longSec/3600))
longitude *= -1
}
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
coord = DMStoDD(latDeg: 41, latMin: 24, latSec: 12.2, latDir: "", longDeg: 2, longMin: 10, longSec: 26.5, longDir: "")
coord.latitude // 41.40338888888889
coord.longitude // 2.174027777777777
And DMM to DD
func DMMtoDD(latDeg: Double, latMin: Double, latDir: String?, longDeg: Double, longMin: Double, longDir: String?) -> CLLocationCoordinate2D {
var latitude = CLLocationDegrees()
if latDeg > 0 {
latitude = CLLocationDegrees(latDeg + ((latMin*60)/3600))
if latDir == "S" {latitude *= -1}
}
else{
latitude = CLLocationDegrees((latDeg * -1) + ((latMin*60)/3600))
latitude *= -1
}
var longitude = CLLocationDegrees()
if longDeg > 0 {
longitude = CLLocationDegrees(longDeg + ((longMin*60)/3600))
if longDir == "W" {longitude *= -1}
}
else{
longitude = CLLocationDegrees((longDeg * -1) + ((longMin*60)/3600))
longitude *= -1
}
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
coord = DMMtoDD(latDeg: 41, latMin: 24.2028, latDir: "N", longDeg: 2, longMin: 10.4418, longDir: "E")
coord.latitude // 41.40338
coord.longitude // 41.40338
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With