2012-10-02 28 views
0

我想找到距selectedAnnotation到userLocation的距离。在我使用的这个问题,以下逻辑尝试didUpdatedidUpdateToLocationiPhone距离当前位置作为字幕

-(void) setDistanceFromCurrentLocation:(CLLocation *)currentLocation{ 

CLLocation *location2 = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude]; 

[self setDistance:[currentLocation distanceFromLocation:location2]]; 
} 

- (NSString *)subtitle 
{ 
NSString *myDistance = [NSString stringWithFormat:@"%1.1f from current location", distance]; 
return myDistance; 
} 

现在:我添加了注释NSObject的下面的代码https://stackoverflow.com/a/10881683/984248

仍然得到0.0回来。我究竟做错了什么?

编辑:

所以我从计算当前位置的距离正确了。但是,我如何通过这个来将它设置为pin的副标题?

这里是我正在寻找距离:

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

[self setCurrentLocation:newLocation]; 

// if not Current Location then update the currently displayed Dealer Annotation 
for (int i=0; i<self.dataArray.count; i++){ 

    NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i]; 
    NSArray *array = [dataDictionary objectForKey:@"Locations"]; 

    for (int i=0; i<array.count; i++){ 

     NSMutableDictionary *dictionary = [array objectAtIndex:i]; 

     CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dictionary objectForKey:@"Latitude"] doubleValue] longitude:[[dictionary objectForKey:@"Longitude"] doubleValue]]; 

     [?????? setDistance:[self.currentLocation distanceFromLocation:pinLocation]]; 
    } 
    } 
} 

这是我如何我添加引脚映射:

for (int i=0; i<self.dataArray.count; i++){ 

    NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i]; 
    NSArray *array = [dataDictionary objectForKey:@"Locations"]; 

    for (int i=0; i<array.count; i++){ 

     NSMutableDictionary *dictionary = [array objectAtIndex:i]; 

     MapAnnotation *annotation = [[MapAnnotation alloc] init]; 

     annotation.latitude = [[dictionary objectForKey:@"Latitude"] doubleValue]; 
     annotation.longitude = [[dictionary objectForKey:@"Longitude"] doubleValue]; 

     CLLocationCoordinate2D coord = {.latitude = 
      annotation.latitude, .longitude = annotation.longitude}; 
     MKCoordinateRegion region = {coord}; 

     annotation.title = [dictionary objectForKey:@"Name"]; 

     annotation.subtitle = ?????; 
     annotation.coordinate = region.center; 

     //Saving the dictionary of the pin to show contact info later 
     annotation.sourceDictionary = dictionary; 
     [mapView addAnnotation:annotation]; 
    } 
} 
+0

不随%1.1F格式化变量你设置self.latitude&self.longitude? – geraldWilliam

+0

我已更新我的问题。 – user984248

回答

0

只是想知道,是currentLocation设置为self.latitude,自我。经度?

在这种情况下,您将试图找到与自己的距离。尝试记录currentLocation参数和self.latitude和self.longitude变量的纬度和经度值,并确保它们不同。

如果是,则尝试记录[currentLocation distanceFromLocation:location2]以查看它是否非零,如果是,则setDistance方法是问题所在。

除此之外,所有我能想到的就是你的距离变量是越来越设置为0别的地方,或者是这样的格式是如何将它设置为0.0

+0

我已更新我的问题。 – user984248

相关问题