0

在应用程序中,我需要计算两点之间的距离。我正在这样做。

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    NSLog(@"updating");
     //Getting my concerts
    arrConcerten = [matches mutableCopy];

       for (Event *concert in arrConcerten) {
            CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];

            CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
            NSLog(@"meters are %f",meters);
            if(meters > 5000){
                [locationManager2 stopUpdatingLocation];
                NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
                NSLog(@"%@",message2);
            }else{

                NSLog(@"too far away");
            }

        }
    }

这就是我的日志所说的。

2013-10-17 15:57:23.518 Domino[7468:907] meters are 7973.753294
2013-10-17 15:57:23.520 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 51.038139 and long = 5.557330
2013-10-17 15:57:23.896 Domino[7468:907] meters are 27850.874245
2013-10-17 15:57:23.899 Domino[7468:907] Long = 5.621456 and lat= 51.097340 and Concert Lat = 50.860332 and long = 5.493722

如您所见,这些位置彼此靠近。但它仍然说我们离得太远了。有人可以帮助我吗?

4

3 回答 3

1

你为什么用这个:

如果超过5000米:做点什么,否则太远?我不明白其中的逻辑。如果距离太远,则超过 5000 米。所以我认为:

  - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation{
        NSLog(@"updating");
         //Getting my concerts
        arrConcerten = [matches mutableCopy];

       for (Event *concert in arrConcerten) {
            CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:[concert.eve_latitude floatValue] longitude:[concert.eve_longitude floatValue]];

            CLLocationDistance meters = [restaurnatLoc distanceFromLocation:newLocation];
            NSLog(@"meters are %f",meters);
            if(meters <= 5000){
                [locationManager2 stopUpdatingLocation];
                NSString *message2 = [NSString stringWithFormat:@"Long = %f and lat= %f and Concert Lat = %f and long = %f",newLocation.coordinate.longitude,newLocation.coordinate.latitude,[concert.eve_latitude floatValue],[concert.eve_longitude floatValue]];
                NSLog(@"%@",message2);
            }else{

                NSLog(@"too far away");
            }

        }
    }

或者更确切地说:

if(meters > 5000){

                    NSLog(@"it's far enough");
 }else{

                    NSLog(@"too close");
 }

还有一件非常重要的事情:

这种方法通过追踪它们之间的一条遵循地球曲率的线来测量两个位置之间的距离。生成的弧线是一条平滑曲线,没有考虑两个位置之间的特定高度变化。来自苹果文档

distanceFromLocation 测量“空中”的距离,而不是逐个测量。

于 2013-10-17T14:16:58.500 回答
0

这是解决方案-

-(NSString *)getDistance:(NSDictionary *)dicVal{
    CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[[dicVal valueForKey:@"latitude"] doubleValue] longitude:[[dicVal valueForKey:@"longitude"] doubleValue]];

    CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:appDelegate.userLocation.latitude longitude:appDelegate.userLocation.longitude];

    CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
    NSString *dist=[NSString stringWithFormat:@"%4.0f %@", distance/1000,NSLocalizedString(@"KM", nil)];
    return dist;
}
于 2013-10-17T14:21:01.667 回答
0

是的,它只是分配问题,您必须分配每个时间cllocation管理器,如下所示:

LocationManager = [[[CLLocationManager alloc] init] autorelease];
LocationManager.delegate = self;
LocationManager.desiredAccuracy = kCLLocationAccuracyBest;
LocationManager.distanceFilter = kCLDistanceFilterNone;
[LocationManager startUpdatingLocation];
CLLocation *location = [LocationManager location];
CLLocationCoordinate2D coordinate1 = [location coordinate];


CLLocation *currentLoc = [[CLLocation alloc] initWithLatitude:coordinate1.latitude longitude:coordinate1.longitude];
CLLocation *restaurnatLoc = [[CLLocation alloc] initWithLatitude:l@"Your latitude not current" longitude:@"Your longitude not current'];
CLLocationDistance meters = [restaurnatLoc distanceFromLocation:currentLoc];    


if (meters > 1000)
{
    Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Km",meters];
}
else{
    Distancelbl.text=[NSString stringWithFormat: @"You are Away %.2f Mtr",meters];
}

`

于 2013-10-21T07:09:33.307 回答