Преобразовать десятичные градусы в градусы Десятичные минуты (DDM)
Я использовал следующие методы для преобразования Decimal Degrees
в Degrees Minutes Seconds (DMS)
, но мне также нужно преобразовать его в Degrees Decimals Minutes (DDM)
,
Как я мог конвертировать Decimal Degrees
DMS
а также DMS
вернуться к Decimal Degrees
?
+ (NSString *)latitudeCoordinate:(CLLocationCoordinate2D)coordinate {
int latSeconds = (int)(coordinate.latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = ABS(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
NSString *result = [NSString stringWithFormat:@"%@ %d° %d' %d\"",
latDegrees >= 0 ? @"N" : @"S",
ABS(latDegrees),
latMinutes,
latSeconds];
return result;
}
+ (NSString *)longitudeCoordinate:(CLLocationCoordinate2D)coordinate {
int longSeconds = (int)(coordinate.longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = ABS(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
NSString* result = [NSString stringWithFormat:@"%@ %d ° %d' %d\"",
longDegrees >= 0 ? @"E" : @"W",
ABS(longDegrees),
longMinutes,
longSeconds
];
return result;
}
+ (double)degreesStringToDecimal:(NSString*)string {
// split the string
NSArray *splitDegs = [string componentsSeparatedByString:@"\u00B0"]; // unicode for degree symbol
NSArray *splitMins = [splitDegs[1] componentsSeparatedByString:@"'"];
NSArray *splitSecs = [splitMins[1] componentsSeparatedByString:@"\""];
// get each segment of the dms string
NSString *degreesString = splitDegs[0];
NSArray *directionArray = [degreesString componentsSeparatedByString:@" "];
NSString *minutesString = splitMins[0];
NSString *secondsString = splitSecs[0];
NSString *direction = directionArray[0];
degreesString = directionArray[1];
// convert degrees
double degrees = [degreesString doubleValue];
// convert minutes
double minutes = [minutesString doubleValue] / 60; // 60 degrees in a minute
// convert seconds
double seconds = [secondsString doubleValue] / 3600; // 60 seconds in a minute, or 3600 in a degree
// add them all together
double decimal = degrees + minutes + seconds;
// determine if this is negative. south and west would be negative values
if ([direction.uppercaseString isEqualToString:@"W"] || [direction.uppercaseString isEqualToString:@"S"])
{
decimal = -decimal;
}
return decimal;
}
1 ответ
Я понял. Вот два метода, которые я использую для lat
а также lon
,
+ (NSString *)DDMfromDEClatitude:(CLLocationDegrees)coordinate {
NSString *coordinateString = [NSString stringWithFormat:@"%f", coordinate];
NSArray *splitCoordinates = [coordinateString componentsSeparatedByString:@"."];
int before = [splitCoordinates[0] intValue];
NSString *after = [NSString stringWithFormat:@"0.%@", splitCoordinates[1]];
double afterDouble = [after doubleValue];
afterDouble = afterDouble * 60;
NSString *direction = before >= 0 ? @"N" : @"S";
if (before < 0) {
before = -before;
}
return [NSString stringWithFormat:@"%@ %i° %.3f", direction, before, afterDouble];
}
+ (NSString *)DDMfromDEClongitude:(CLLocationDegrees)coordinate {
NSString *coordinateString = [NSString stringWithFormat:@"%f", coordinate];
NSArray *splitCoordinates = [coordinateString componentsSeparatedByString:@"."];
int before = [splitCoordinates[0] intValue];
NSString *after = [NSString stringWithFormat:@"0.%@", splitCoordinates[1]];
double afterDouble = [after doubleValue];
afterDouble = afterDouble * 60;
NSString *direction = before >= 0 ? @"E" : @"W";
if (before < 0) {
before = -before;
}
return [NSString stringWithFormat:@"%@ %i° %.3f", direction, before, afterDouble];
}
Вот метод конвертации обратно из DDM
в DEC
:
+ (double)DECfromDDM:(NSString *)ddmString {
NSArray *split = [ddmString componentsSeparatedByString:@"\u00B0 "];
NSArray *splitDegs = [split[0] componentsSeparatedByString:@" "];
double minutes = [split[1] doubleValue] / 60;
NSString *directionString = splitDegs[0];
int direction = [splitDegs[1] intValue];
double coordinate = direction + minutes;
if ([directionString.uppercaseString isEqualToString:@"W"] || [directionString.uppercaseString isEqualToString:@"S"]) {
coordinate = -coordinate;
}
return coordinate;
}
Я использовал конвертер координат, чтобы проверить правильность моих расчетов: http://www.pgc.umn.edu/tools/conversion