Swift将十进制坐标转换为度,分,秒,方向

时间:2015-01-17 05:06:12

标签: swift gps xcode6

我怎样才能将其转换为快速?我最好的猜测是所有的int都变成了var。删除所有@那个领导"。此外,如果有任何可以指向一个很好的来源,以了解事情如何转换,这将是伟大的。

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

我尝试转换它但Xcode证明我错了。将修复建议与ABS重新发布。现在看起来是否正确?

func coordinateString {

var latSeconds = (Int8)(self.latitude * 3600);
var latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
var latMinutes = latSeconds / 60;
latSeconds %= 60;

var longSeconds = (Int8)(self.longitude * 3600);
var longDegrees = longSeconds / 3600;
longSeconds = abs(longSeconds % 3600);
var longMinutes = longSeconds / 60;
longSeconds %= 60;

    var result = (String(format: "%d°%d'%d\"%@ %d°%d'%d\"%@"),
abs(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? "N" : "S",
abs(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? "E" : "W",

return result;
}

5 个答案:

答案 0 :(得分:15)

Xcode 8.3.2•Swift 3.1

func coordinateString(_ latitude: Double,_ longitude: Double) -> String {
    var latSeconds = Int(latitude * 3600)
    let latDegrees = latSeconds / 3600
    latSeconds = abs(latSeconds % 3600)
    let latMinutes = latSeconds / 60
    latSeconds %= 60
    var longSeconds = Int(longitude * 3600)
    let longDegrees = longSeconds / 3600
    longSeconds = abs(longSeconds % 3600)
    let longMinutes = longSeconds / 60
    longSeconds %= 60
    return String(format:"%d°%d'%d\"%@ %d°%d'%d\"%@",
                  abs(latDegrees),
                  latMinutes,
                  latSeconds, latDegrees >= 0 ? "N" : "S",
                  abs(longDegrees),
                  longMinutes,
                  longSeconds,
                  longDegrees >= 0 ? "E" : "W" )
}


coordinateString(-22.9133950, -43.2007100)   // "22°54'48"S 43°12'2"W"

答案 1 :(得分:3)

我刚刚编辑了 Leo Dabus 的答案,将这些字符串作为单独的值。如果它对其他人有帮助:

Swift 3和Swift 4

func getLocationDegreesFrom(latitude: Double) -> String {

    var latSeconds = Int(latitude * 3600)

    let latDegrees = latSeconds / 3600

    latSeconds = abs(latSeconds % 3600)

    let latMinutes = latSeconds / 60

    latSeconds %= 60

    return String(

        format: "%d°%d'%d\"%@",
        abs(latDegrees),
        latMinutes,
        latSeconds,
        latDegrees >= 0 ? "N" : "S"

    )

}

func getLocationDegreesFrom(longitude: Double) -> String {

    var longSeconds = Int(longitude * 3600)

    let longDegrees = longSeconds / 3600

    longSeconds = abs(longSeconds % 3600)

    let longMinutes = longSeconds / 60

    longSeconds %= 60

    return String(

        format: "%d°%d'%d\"%@",
        abs(longDegrees),
        longMinutes,
        longSeconds,
        longDegrees >= 0 ? "E" : "W"

    )

}

答案 2 :(得分:2)

当纬度或经度的值小于零时,我拿了Leo Dabus的代码并修复与检测基本方向相关的错误,I当我们在南半球 在本初子午线以西时意味着什么。

我们不能用Swift表示负零,所以我们在以下表达式中丢失了负号:

let latDegrees = latSeconds / 3600

这个错误也使得表达:

latDegrees >= 0 ? "N" : "S"

始终返回N(北),而不是S(南)。

以下是我的代码版本( Swift 5和Xcode 10 ):

func coordinate() -> (latitude: String, longitude: String) {

    // This function converts from DD (decimal degrees) to DMS (degrees, minutes and seconds)

    // Calculating the degrees, minutes and seconds for the given latitude value (DD)

    var latitudeSeconds = latitude * 3600

    let latitudeDegrees = latitudeSeconds / 3600

    latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)

    let latitudeMinutes = latitudeSeconds / 60

    latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)

    // Calculating the degrees, minutes and seconds for the given longitude value (DD)

    var longitudeSeconds = longitude * 3600

    let longitudeDegrees = longitudeSeconds / 3600

    longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)

    let longitudeMinutes = longitudeSeconds / 60

    longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)

    // Analyzing if it's North or South. (Latitude)

    let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"

    // Analyzing if it's East or West. (Longitude)

    let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"

    // Final strings with format <degrees>°<minutes>'<seconds>"<cardinal direction>

    let latitudeDescription = String(format:"%.2f°%.2f'%.2f\"%@",
                                     abs(latitudeDegrees), abs(latitudeMinutes),
                                     abs(latitudeSeconds), latitudeCardinalDirection)

    let longitudeDescription = String(format:"%.2f°%.2f'%.2f\"%@",
                                      abs(longitudeDegrees), abs(longitudeMinutes),
                                      abs(longitudeSeconds), longitudeCardinalDirection)

    return (latitudeDescription, longitudeDescription)

} // coordinate

我还选择使用Double来处理精度,例如,我更喜欢显示两位小数。

此代码的屏幕输出类似于:

  

0.17°10.34'20.53“S 78.48°28.59'35.52”W

答案 3 :(得分:0)

基于David Seek和JosuéV。Herrera代码

func latlon2DMS(latitude: Double) -> String {
    var latitudeSeconds = latitude * 3600
    let latitudeDegrees = latitudeSeconds / 3600
    latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)
    let latitudeMinutes = latitudeSeconds / 60
    latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)
    let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
    let latitudeDescription = String(format: "%.2f° %.2f' %.2f\" %@",
                                     abs(latitudeDegrees), abs(latitudeMinutes),
                                     abs(latitudeSeconds), latitudeCardinalDirection)
    return latitudeDescription
}

func latlon2DMS(longitude: Double) -> String {
    var longitudeSeconds = longitude * 3600
    let longitudeDegrees = longitudeSeconds / 3600
    longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)
    let longitudeMinutes = longitudeSeconds / 60
    longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)
    let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"
    let longitudeDescription = String(format: "%.2f° %.2f' %.2f\" %@",
                                      abs(longitudeDegrees), abs(longitudeMinutes),
                                      abs(longitudeSeconds), longitudeCardinalDirection)
    return longitudeDescription
}

使用

调用它
print(latlon2DMS(latitude: coordLat))
print(latlon2DMS(longitude: coordLong))

latitude.text = latlon2DMS(latitude: coordLat)
longitude.text = latlon2DMS(longitude: coordLong)

答案 4 :(得分:0)

我略微修改了Josue V.的代码,使其可以在计时器中工作,该计时器会经常更新位置

        if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
            CLLocationManager.authorizationStatus() ==  .authorizedAlways){
        let currentLocation = locationManager.location
        var latitudeSeconds = currentLocation!.coordinate.latitude * 3600
        let latitudeDegrees = latitudeSeconds / 3600

            latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)

        let latitudeMinutes = latitudeSeconds / 60

            latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)

        var longitudeSeconds = currentLocation!.coordinate.longitude * 3600
        let longitudeDegrees = longitudeSeconds / 3600

            longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)

        let longitudeMinutes = longitudeSeconds / 60

            longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)

        let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
        let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"

        let latitudeDescription = String(format:"%.2f°%.2f'%.2f\"%@",
                                         abs(latitudeDegrees), abs(latitudeMinutes),
                                         abs(latitudeSeconds), latitudeCardinalDirection)

        let longitudeDescription = String(format:"%.2f°%.2f'%.2f\"%@",
                                          abs(longitudeDegrees), abs(longitudeMinutes),
                                          abs(longitudeSeconds), longitudeCardinalDirection)

        cell5.text = latitudeDescription + " " + longitudeDescription

        }
    }

和计时器

timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(coordinate), userInfo: nil, repeats: true)