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