Swift将十进制坐标转换为度,分,秒,方向 [英] Swift Convert decimal coordinate into degrees, minutes, seconds, direction

查看:129
本文介绍了Swift将十进制坐标转换为度,分,秒,方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能把这个转化为迅速?我最好的猜测是,所有的int都变成了var。删除所有的@领导。如果有任何可以指示我一个很好的来源,以了解如何转换这将是伟大的。

How can I covert this to swift? My best guess is that all the int get changed to a var. Removing all the @ that lead ". Also if any can point me to a good source to learn how things convert that would great.

- (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。它现在看起来是否正确?

My attempt to convert it but Xcode proves me wrong. Reposted the fix suggest with the ABS. Does it look correct now?

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


推荐答案

Xcode 8.3.2•Swift 3.1 b

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"

这篇关于Swift将十进制坐标转换为度,分,秒,方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆