2012-08-09 36 views
2

我需要格式化我的座標是這樣的:如何格式化這樣的座標?

N 61° 14.5919' 
E 23° 28.1751' 

現在我的座標是這樣的:

61.145919 
23.281751 

我的代碼:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    latitudeLabel.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]; 
    longitudeLabel.text = [NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]; 
} 
+2

[這] [1]的信息可以幫助你。請享用! [1]:http://stackoverflow.com/questions/8851816/convert-decimal-coordinate-into-degrees-minutes-seconds-direction – parilogic 2012-08-09 07:42:34

回答

0

這並不容易。

在你的例子中,你已經有61度的北部和14.5919分鐘,沒有秒。 (秒鐘被分鐘中的小數重新識別。)

您必須將分鐘轉換爲釐米。

61 + 14.5919/60將導致61.31983。

您將不得不考慮S以緯度爲負值以及W爲經度負值。
您的示例恰好具有N和E座標,這對正值很有效。但你不能認爲這是理所當然的。至少不是如果你想要一些通用的解決方案。

因此對於緯度: 如果該值> = 0,則爲N,否則爲S. 您可以截斷度數的逗號後值並添加°。 然後取其餘部分並乘以60,並將其用作分鐘並添加th'。

.145919 * 60 = 8.75514 - 這是本例中的分鐘值。這應該會導致 N 61°8.75514'

由於您的問題與編程問題無關,而與後面的數學有關,所以我會讓您從那裏開始。

0

請參見下面的代碼

int degreesLat = marker.position.latitude; 
double decimalLat = fabs(marker.position.latitude-degreesLat); 
int minutesLat = decimalLat*60; 
double secondsLat = decimalLat*3600 - minutesLat*60;