显示从用户位置到其他mapview注释的距离,但计算的距离为0.0000?

时间:2013-02-04 03:49:02

标签: objective-c annotations mkmapview

我正在尝试在标签(位于TableViewController上)中显示用户当前位置到其他mapview注释(从我的MapViewController拉出)的距离,但由于某种原因,计算出的距离标签在运行时显示为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

{


}

1 个答案:

答案 0 :(得分:2)

似乎这一行:

    CLLocationDistance calculatedDistance = [userLocation distanceFromLocation:userLocation];

计算与自身的距离?

之前的行似乎正确地计算了注释距离,但是你在...之后的行中覆盖了它...

相关问题