Uitableview 计算用户与设备之间的距离;附近的位置,计算出的距离一直显示为0?

Uitableview 计算用户与设备之间的距离;附近的位置,计算出的距离一直显示为0?,uitableview,mkmapview,mkannotation,xcode4.6,Uitableview,Mkmapview,Mkannotation,Xcode4.6,我使用以下代码来计算我的应用程序用户的位置和附近位置(注释)之间的距离。当我试图将“距离”显示为标签中的文本时,它总是说计算的距离为0。为什么会这样 *请注意,我的表格使用MapView上的坐标来确定距离 这是我用来计算从我的用户到附近位置的距离的代码 MapViewController.m - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation from

我使用以下代码来计算我的应用程序用户的位置和附近位置(注释)之间的距离。当我试图将“距离”显示为标签中的文本时,它总是说计算的距离为0。为什么会这样

*请注意,我的表格使用MapView上的坐标来确定距离

这是我用来计算从我的用户到附近位置的距离的代码

MapViewController.m

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

   for (MapViewAnnotation *annotation in self.mapView.annotations) {
CLLocationCoordinate2D coord = [annotation coordinate];
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
annotation.distance = [newLocation distanceFromLocation:annotationLocation];



    }
cell.distanceLabel.text = [NSString stringWithFormat:@"%f m.", calculatedDistance];
下面是我用来在标签中显示计算距离的位(位于要在tableview中显示的自定义单元格中):

ViewController.m

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

   for (MapViewAnnotation *annotation in self.mapView.annotations) {
CLLocationCoordinate2D coord = [annotation coordinate];
CLLocation *annotationLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
annotation.distance = [newLocation distanceFromLocation:annotationLocation];



    }
cell.distanceLabel.text = [NSString stringWithFormat:@"%f m.", calculatedDistance];
任何帮助都将不胜感激

for (MapViewAnnotation *annotation in self.mapView.annotations) {
  CLLocationCoordinate2D coord = [annotation coordinate];
  CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:coord.latitude longitude:coord.longitude];
这不是用户的位置,而是注释的位置。但撇开奇怪的变量名不谈:

CLLocationDistance calculatedDistance = [userLocation distanceFromLocation:userLocation];
从一个位置到其自身的距离为0那句话可能是个错误。也许你的意思是

CLLocationDistance calculatedDistance = [newLocation distanceFromLocation:userLocation];
这将返回用户的新位置和(命名错误)注释位置之间的距离-可能是您想要的。当然,你实际上只计算了自我计算距离上方的直线,然后在两行之后扔掉它

tl;博士
扔掉最后两行,它们没用。

我扔掉了最后两行,并更改了变量名(lol)。请参阅上面更新的代码。这就是我的结局吗?我不需要定义计算的距离吗?谢谢你的帮助,顺便说一句!只要把它(距离)从显示代码中的注释中拉出来,或者别的什么。(此外,单个变量
calculatedDistance
不足以保持多个距离(正如在
for
循环中要求它做的那样)。)好的,我现在完全明白为什么我的最后两行没有用了,哈哈。你能告诉我w/code这会是什么样子吗?我有点糊涂了!Thx:)