I am able to determine the latitude and longitude of a location in the center of a map with the following:
func TargetGridReference(outletMapView_PrimaryTargetLo
accepted answer sometimes return invalid values, coordinateToDMS(latitude: 12.99999999, longitude: 0.0)
returns latitude 12° 59' 60.0000" N
enum GeoFormat {
case D
case DM
case DMS
}
func dms(value: Double, format: GeoFormat)->String {
let sign = value < 0.0 ? -1 : 1
let value = abs(value)
switch format {
case .D:
return String(format: "%+15.10f°", value * Double(sign))
case .DM:
var deg = Int(value)
var min = round((value - Double(deg)) * 60 * 10000000) * 0.0000001
if min < 60.0 {} else {
min -= 60.0
deg += 1
}
return String(format: "%+4d°%010.7f′", deg * sign, min)
case .DMS:
var deg = Int(value)
let _min = (value - Double(deg)) * 60
var min = Int(_min)
var sec = round((_min - Double(min)) * 60 * 10000) * 0.0001
if sec < 60.0 {} else {
sec -= 60.0
min += 1
}
if min < 60 {} else {
min -= 60
deg += 1
}
return String(format: "%+4d°%02d′%07.4f″", deg * sign, min, sec)
}
}
let value = -12.999999997222222
print(dms(value: value, format: .D))
print(dms(value: value, format: .DM))
print(dms(value: value, format: .DMS))
prints, as required
-12.9999999972°
-12°59.9999998′
-13°00′00.0000″