Convert decimal coordinate into degrees, minutes, seconds, direction

前端 未结 6 1631
遥遥无期
遥遥无期 2020-12-28 21:13

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

6条回答
  •  被撕碎了的回忆
    2020-12-28 21:51

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

提交回复
热议问题