English 中文(简体)
Con 调为学位、分钟、秒、方向
原标题:Convert decimal coordinate into degrees, minutes, seconds, direction

迄今为止,我有以下几点,但若发言,我可以 figure忙地获得指示函。 任何想法? 理想的情况是,我要扩大拥有某种类别的CLocation类别。

-(NSString *)nicePosition{

double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];

int latSeconds = (int)round(latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;

int longSeconds = (int)round(longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = abs(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;

//TODO: Use N,E,S,W notation in lat/long

return [NSString stringWithFormat:@"%i° %i  %i", %i° %i  %i"", latDegrees, latMinutes, latSeconds, longDegrees, longMinutes, longSeconds];
}

关于记录,我做了以下工作。

-(NSString *)nicePosition{

double latitude = [self.latitude doubleValue];
double longitude = [self.longitude doubleValue];

int latSeconds = (int)round(abs(latitude * 3600));
int latDegrees = latSeconds / 3600;
latSeconds = latSeconds % 3600;
int latMinutes = latSeconds / 60;
latSeconds %= 60;

int longSeconds = (int)round(abs(longitude * 3600));
int longDegrees = longSeconds / 3600;
longSeconds = longSeconds % 3600;
int longMinutes = longSeconds / 60;
longSeconds %= 60;

char latDirection = (latitude >= 0) ?  N  :  S ;
char longDirection = (longitude >= 0) ?  E  :  W ;

return [NSString stringWithFormat:@"%i° %i  %i" %c, %i° %i  %i" %c", latDegrees, latMinutes, latSeconds, latDirection, longDegrees, longMinutes, longSeconds, longDirection];
}
最佳回答

标准方式:

char lonLetter = (lon > 0) ?  E  :  W ;
char latLetter = (lat > 0) ?  N  :  S ;
问题回答

这里有一些基于以上丹尼尔解决方案的目标C:

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

C#中的解决办法:

    void Run(double latitude, double longitude)
    {
        int latSeconds = (int)Math.Round(latitude * 3600);
        int latDegrees = latSeconds / 3600;
        latSeconds = Math.Abs(latSeconds % 3600);
        int latMinutes = latSeconds / 60;
        latSeconds %= 60;

        int longSeconds = (int)Math.Round(longitude * 3600);
        int longDegrees = longSeconds / 3600;
        longSeconds = Math.Abs(longSeconds % 3600);
        int longMinutes = longSeconds / 60;
        longSeconds %= 60;

        Console.WriteLine("{0}° {1}  {2}" {3}, {4}° {5}  {6}" {7}",
            Math.Abs(latDegrees),
            latMinutes,
            latSeconds,
            latDegrees >= 0 ? "N" : "S",
            Math.Abs(longDegrees),
            longMinutes,
            longSeconds,
            latDegrees >= 0 ? "E" : "W");
    }

例如:

new Program().Run(-15.14131211, 56.345678);
new Program().Run(15.14131211, -56.345678);
new Program().Run(15.14131211, 56.345678);

印刷:

15° 8  29" S, 56° 20  44" W
15° 8  29" N, 56° 20  44" E
15° 8  29" N, 56° 20  44" E

希望这样做,是正确的。 亲爱!

如果你想迅速这样做,你可以做这样的事情:

import MapKit

extension CLLocationCoordinate2D {

    var latitudeDegreeDescription: String {
        return fromDecToDeg(self.latitude) + " (self.latitude >= 0 ? "N" : "S")"
    }
    var longitudeDegreeDescription: String {
        return fromDecToDeg(self.longitude) + " (self.longitude >= 0 ? "E" : "W")"
    }
    private func fromDecToDeg(input: Double) -> String {
        var inputSeconds = Int(input * 3600)
        let inputDegrees = inputSeconds / 3600
        inputSeconds = abs(inputSeconds % 3600)
        let inputMinutes = inputSeconds / 60
        inputSeconds %= 60
        return "(abs(inputDegrees))°(inputMinutes) (inputSeconds)  "
    }
}

关于亚历克的答复,这里是迅速3的解决办法,其产出图。 它用图归还坐标。

此外,它实际上扩大了CLLocationDegrees的类别,并且不需要额外的参数。

import MapKit

extension CLLocationDegrees {

    func degreeRepresentation() -> (northOrEast: Bool, degrees: Int, minutes: Int, seconds: Int) {
        var inputSeconds = Int(self * 3600)
        let inputDegrees = inputSeconds / 3600
        inputSeconds = abs(inputSeconds % 3600)
        let inputMinutes = inputSeconds / 60
        inputSeconds %= 60

        return (inputDegrees > 0, abs(inputDegrees), inputMinutes, inputSeconds)
    }

}
int latSeconds = (int)round(abs(latitude * 3600));

This is mistake! Proper is

int latSeconds = abs(round(latitude * 3600));  




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签