2011-03-08 83 views
0

`你好,我使用distanceFromLocation方法時,它總是返回零的問題。CLLocation distanceFromLocation always return 0

這裏是我的代碼片段:

NSArray *arrATMItem = [arrATMs objectAtIndex:indexPath.row]; 
cell.textLabel.text = [arrATMItem objectAtIndex:0]; 

float lat = [[arrATMItem objectAtIndex:1] floatValue]; 
float lon = [[arrATMItem objectAtIndex:2] floatValue]; 
CLLocation *branchLoc = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; 

CLLocationDistance dist = [currentLocation distanceFromLocation:branchLoc]; 
NSLog(@"distance = %d",dist); 
cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"%d m", dist]; 

我還檢查了branchLoc和currentLoc實例具有正確的價值 這裏的branchLog LAT的例子值:-6.17503957長:106.79891717 和currentLoc值LAT :-6.17603957長:106.79891717

branchLoc值從plist文件中檢索,而currentLoc是從我的委託文件中定義的CLLocation locationUpdate方法的CLLocation類中獲取的。

我試過使用%f,它也返回類似於0.00000 的任何想法,我做錯了什麼?

謝謝

+0

我爲其他人添加此評論,因爲這是發生在我身上的事情。我正在使用我製作的測試位置。然而,他們是無效的(我混合了拉特和長)。您可以使用函數CLLocationCoordinate2DIsValid(currentLocation.coordinate)來確定座標是否有效。 – Roberto 2016-02-11 18:03:42

回答

0

嘗試使用double(或CLLocationDegrees)經度和緯度。

相關問題