CLLocation距离位置始终返回0
我在使用 distanceFromLocation
方法时遇到问题,它总是返回零。
这是我的代码片段:
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
的示例值:
lat: -6.17503957 long: 106.79891717
currentLoc
的值为:
lat: -6.17603957 long: 106.79891717
branchLoc
值是从 plist 文件中检索的,而 currentLoc
是从我的委托文件中定义的 CLLocation locationUpdate
方法上的 CLLocation 类获取的。
我尝试使用 %f ,它也返回类似 0.00000 的值。
知道我做错了什么吗?
谢谢
I'm having a problem when using the distanceFromLocation
method, where it always returns zero.
Here's my code snippet:
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];
I have also checked that the branchLoc
and currentLoc
instances have the right values.
Here's an example value of branchLoc
:
lat: -6.17503957 long: 106.79891717
And a value for currentLoc
is:
lat: -6.17603957 long: 106.79891717
The branchLoc
values are retrieved from a plist file, and the currentLoc
is obtained from the CLLocation class on the CLLocation locationUpdate
method defined in my delegate file.
I tried using %f , and it also returns something like 0.00000.
Any idea what I'm doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 double(或 CLLocationDegrees)表示纬度和经度。
Try using double (or CLLocationDegrees) for lat and lon.