【问题标题】:Convert Decimal Degrees to Degrees Decimal Minutes (DDM)将十进制度数转换为十进制度分 (DDM)
【发布时间】:2015-09-15 07:36:17
【问题描述】:

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

如何将Decimal Degrees DMSDMS 转换回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;
}

【问题讨论】:

  • 度数和小数分钟,例如 27 度、14.241 分钟(而不是度/分/秒?

标签: ios objective-c math cllocation


【解决方案1】:

我想通了。这是我用于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

【讨论】:

  • 我用过上面的方法,效果很好。但是,如果您的位置在 0° E/W 或 N/S 左右,则会出现问题;如果“int before”是-0.xxx,则它不能正确,这将导致错误的 N/S 或 E/W 坐标。所以为了计算方向字符串,我用原始坐标进行测试。
猜你喜欢
  • 2015-01-09
  • 2013-10-27
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多