Frage

Ich habe das Folgende bisher, kann aber nicht ordentlich herausfinden, um die Richtungsbriefe ohne eine Haufen unordentlicher wenn auch Aussagen einzubeziehen. Irgendwelche Ideen? Idealerweise möchte ich die Cllocationsklasse mit einer Kategorie erweitern, um dies zu tun.

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

Für die Aufzeichnung habe ich Folgendes gemacht.

-(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];
}
War es hilfreich?

Lösung

Standardweg:

char lonLetter = (lon > 0) ? 'E' : 'W';
char latLetter = (lat > 0) ? 'N' : 'S';

Andere Tipps

Hier ist ein Ziel-C, das auf Daniels Lösung oben basiert:

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

Hier ist eine Lösung in 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");
    }

Dies ist ein Beispiellauf:

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

Welche druckt:

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

Hoffe das hilft und dass es das Richtige tut. Viel Glück!

Wenn Sie es in Swift tun möchten, können Sie so etwas machen:

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)''"
    }
}

In Bezug auf Alex 'Antwort finden Sie hier eine Lösung in Swift 3 mit einem Ausgangstupel. Es gibt die Koordinaten in einem Tupel zurück.

Darüber hinaus erweitert es die Klasse CllocationDegrees wirklich und erfordert keinen zusätzlichen Parameter.

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

Das ist Fehler! Ordentlich ist

int latSeconds = abs(round(latitude * 3600));  
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top