2013-02-04 37 views
-2

我试图显示从用户的当前位置到其他mapview注释(从我的MapViewController拉)在标签(位于TableViewController)的距离,但由于某些原因计算的距离标签说0.0000当我运行时模拟器?即使在我定制了我的“当前位置”之后。任何想法为什么?显示从用户位置到其他mapview注释的距离,但计算的距离为0.0000?

MapViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 


@interface MapViewController : UIViewController <MKMapViewDelegate> 

    @property (nonatomic, strong) IBOutlet MKMapView *mapView; 
    @property (nonatomic, retain) NSMutableArray *dispensaries; 
    @property (nonatomic, retain) NSMutableData *data; 



@end 

MapViewController.m

[super viewDidUnload]; 



     NSLog(@"Getting Device Locations"); 

     NSString *hostStr = @"http://stylerepublicmagazine.com/dispensaries.php"; 
     NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:hostStr]]; 
     NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 
     NSLog(@"server output: %@", serverOutput); 
     NSMutableArray *array = [[[serverOutput objectFromJSONString] mutableCopy] autorelease]; 
     dispensaries = [serverOutput objectFromJSONString]; 
     NSLog(@"%@", [serverOutput objectFromJSONString]); 
     for (NSDictionary *dictionary in array) { 
      assert([dictionary respondsToSelector:@selector(objectForKey:)]); 


      CLLocationCoordinate2D coord = {[[dictionary objectForKey:@"lat"] doubleValue], [[dictionary objectForKey:@"lng"] doubleValue]}; 

      MapViewAnnotation *ann = [[MapViewAnnotation alloc] init]; 
      ann.title = [dictionary objectForKey:@"Name"]; 
      ann.subtitle = [dictionary objectForKey:@"Address1"]; 
      ann.coordinate = coord; 
      [mapView addAnnotation:ann]; 


    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 



    self.mapView.delegate = self; 


} 

} 


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 

{ 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 2000, 2000); 
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 


MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
point.coordinate = userLocation.coordinate; 
point.title = @"You Are Here"; 
point.subtitle = @"Your current location"; 

[self.mapView addAnnotation:point]; 


} 


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

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

    NSArray *sortedArray; 
    sortedArray = [self.mapView.annotations sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { 
     NSNumber *first = [NSNumber numberWithDouble:[(MapViewAnnotation*)a distance]]; 
     NSNumber *second = [NSNumber numberWithDouble:[(MapViewAnnotation*)b distance]]; 
     return [first compare:second]; 
    }]; 


} 

TableViewController.h

#import <UIKit/UIKit.h> 

#import "MapViewController.h" 


@interface TableViewController : UITableViewController { 


    IBOutlet UITableView *DispensaryTableView; 

    NSArray *dispensaries; 
    NSMutableData *data; 


} 

@property CLLocationDistance calculatedDistance; 






@end 

TableViewController.m

 [super viewDidLoad]; 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSURL *url = [NSURL URLWithString:@"http://stylerepublicmagazine.com/dispensaries.php"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    data = [[NSMutableData alloc] init]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{ 
    [data appendData:theData]; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    dispensaries = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
    [DispensaryTableView reloadData]; 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure that you're connected to 3G or Wi-Fi." delegate:nil 
               cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [errorView show]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 


} 
- (int)numberOfSectionsInTableView: (UITableView *)tableview 

{ 
    return 1; 

} 

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dispensaries count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    static NSString *DispensaryTableIdentifier = @"DispensaryCell"; 

    DispensaryCell *cell = (DispensaryCell *)[tableView dequeueReusableCellWithIdentifier:DispensaryTableIdentifier]; 
    if (cell == nil) 

    { 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DispensaryCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.nameLabel.text = [[dispensaries objectAtIndex:indexPath.row] objectForKey:@"Name"]; 
    cell.distanceLabel.text = [NSString stringWithFormat:@"%f", calculatedDistance]; 



      return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{ 


} 
+0

您需要更具体一点上你的问题,或者你是不同于获得满意的答案。你的代码是什么样的(只是相关的行)? – Floris

+2

微不足道的建议是“你正在显示从一个点到它自己的距离”,但没有代码,我们不能告诉你为什么这是... – Floris

+0

对不起,刚更新Q代码。 – user1953744

回答

2

看来,这条线:以自身

CLLocationDistance calculatedDistance = [userLocation distanceFromLocation:userLocation]; 

单位计算距离?

似乎之前正确计算标注距离,但你覆盖它在之后的行的行...