gpt4 book ai didi

ios - 将十进制度数转换为十进制度分 (DDM)

转载 作者:行者123 更新时间:2023-11-29 01:39:29 24 4
gpt4 key购买 nike

我一直在使用以下方法将Decimal Degrees转换为Degrees Minutes Seconds (DMS),但我还需要将其转换为Degrees Decimals Minutes (DDM)

如何将十进制度 DMSDMS 转换回十进制度

+ (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;
}

最佳答案

我想通了。以下是我用于 latlon 的两种方法。

+ (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

关于ios - 将十进制度数转换为十进制度分 (DDM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32574795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com