2013-02-04 32 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]; 

單位計算距離?

似乎之前正確計算標註距離,但你覆蓋它在之後的行的行...