将十进制坐标转换为度、分、秒、方向
Posted
技术标签:
【中文标题】将十进制坐标转换为度、分、秒、方向【英文标题】:Convert decimal coordinate into degrees, minutes, seconds, direction 【发布时间】:2012-01-13 14:09:19 【问题描述】:到目前为止,我有以下内容,但是如果没有一堆凌乱的 if 语句,我想不出一个整洁的方法来获取方向字母。有任何想法吗?理想情况下,我想用一个类别扩展 CLLocation 类来做到这一点。
-(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];
【问题讨论】:
我是密集的还是只是决定 NESW 的 long/lat 的数字符号?然后你就可以使用:NSLog(@"%@%@", (lat > 0) ? @"N" : (lat < 0) ? @"S" : @"", (long > 0) ? @"E" : (long < 0) ? @"W");
我想仍然有一个“混乱的 ifs”元素..
【参考方案1】:
标准方式:
char lonLetter = (lon > 0) ? 'E' : 'W';
char latLetter = (lat > 0) ? 'N' : 'S';
【讨论】:
是的,这就是我最终要做的。你先回答。 :)【参考方案2】:以下是基于 Daniel 上述解决方案的一些 Objective-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;
【讨论】:
在 swift 中看起来如何? 为什么要截断(使用(int)(coordinate * 3600)
)而不是像Daniel Lidström 那样四舍五入以获得秒数?【参考方案3】:
这是 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
希望这会有所帮助,并且它会做正确的事。祝你好运!
【讨论】:
【参考方案4】:如果你想快速完成,你可以做这样的事情:
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)''"
【讨论】:
【参考方案5】:关于 Alex 的回答,这是 Swift 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)
【讨论】:
【参考方案6】:int latSeconds = (int)round(abs(latitude * 3600));
这是错误的!正确的是
int latSeconds = abs(round(latitude * 3600));
【讨论】:
请解释原因。以上是关于将十进制坐标转换为度、分、秒、方向的主要内容,如果未能解决你的问题,请参考以下文章