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

时间:2015-09-14 22:15:21

标签: ios objective-c math cllocation

我一直在使用以下方法将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;
}

1 个答案:

答案 0 :(得分: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