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
If you want to do it in swift you can make something like that:
import MapKit
extension CLLocationCoordinate2D {
var latitudeDegreeDescription: String {
return fromDecToDeg(self.latitude) + " \(self.latitude >= 0 ? "N" : "S")"
}
var longitudeDegreeDescription: String {
return fromDecToDeg(self.longitude) + " \(self.longitude >= 0 ? "E" : "W")"
}
private func fromDecToDeg(input: Double) -> String {
var inputSeconds = Int(input * 3600)
let inputDegrees = inputSeconds / 3600
inputSeconds = abs(inputSeconds % 3600)
let inputMinutes = inputSeconds / 60
inputSeconds %= 60
return "\(abs(inputDegrees))°\(inputMinutes)'\(inputSeconds)''"
}
}
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)
}
}
Here's some Objective-C based on Daniel's solution above:
- (NSString*)coordinateString {
int latSeconds = (int)(self.latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = ABS(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)(self.longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = ABS(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
NSString* result = [NSString stringWithFormat:@"%d°%d'%d\"%@ %d°%d'%d\"%@",
ABS(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? @"N" : @"S",
ABS(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? @"E" : @"W"];
return result;
}
Here's a solution in C#:
void Run(double latitude, double longitude)
{
int latSeconds = (int)Math.Round(latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = Math.Abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)Math.Round(longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = Math.Abs(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
Console.WriteLine("{0}° {1}' {2}\" {3}, {4}° {5}' {6}\" {7}",
Math.Abs(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? "N" : "S",
Math.Abs(longDegrees),
longMinutes,
longSeconds,
latDegrees >= 0 ? "E" : "W");
}
This is an example run:
new Program().Run(-15.14131211, 56.345678);
new Program().Run(15.14131211, -56.345678);
new Program().Run(15.14131211, 56.345678);
Which prints:
15° 8' 29" S, 56° 20' 44" W
15° 8' 29" N, 56° 20' 44" E
15° 8' 29" N, 56° 20' 44" E
Hope this helps, and that it does the right thing. Good luck!
Standard way:
char lonLetter = (lon > 0) ? 'E' : 'W';
char latLetter = (lat > 0) ? 'N' : 'S';
int latSeconds = (int)round(abs(latitude * 3600));
This is mistake! Proper is
int latSeconds = abs(round(latitude * 3600));