【发布时间】:2011-07-10 20:15:47
【问题描述】:
我在使用distanceFromLocation 方法时遇到问题,它总是返回零。
这是我的代码 sn-p:
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 实例的值是否正确。
这是branchLoc 的示例值:
纬度:-6.17503957 经度:106.79891717
currentLoc 的值是:
纬度:-6.17603957 经度:106.79891717
branchLoc 值是从 plist 文件中检索的,currentLoc 是从我的委托文件中定义的 CLLocation locationUpdate 方法上的 CLLocation 类获得的。
我尝试使用 %f ,它也返回类似 0.00000 的值。
知道我做错了什么吗?
谢谢
【问题讨论】:
-
我正在为其他人添加此评论,因为这就是发生在我身上的事情。我使用的是我自己编造的测试地点。但是,它们是无效的(我混淆了经纬度)。可以使用
CLLocationCoordinate2DIsValid(currentLocation.coordinate)函数判断坐标是否有效。
标签: cllocation