iphone - distanceFromLocation 似乎不起作用

标签 iphone ios objective-c location cllocationmanager

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

- (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

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

最佳答案

你为什么要用这个:

如果超过 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");
 }

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

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. from apple documentation

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

关于iphone - distanceFromLocation 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19428832/

相关文章:

android - 将一个 LabelView 动态放置在另一个 LabelView 下

iphone - 使用 iphone 上的 HTML5 视频元素,如何检测 "pause"和 "done"之间的差异?

objective-c - Objective-C 中 @property 声明中的 "getter"关键字?

ios - 来自 xib 的 View 存在奇怪的约束/UI 错误

iphone - 如何创建 pinterest 样式隐藏/取消隐藏导航/标签栏?

iphone - 将 iPhone 应用移植到 iPad

ios - 调用 CFReadStreamRead 停止线程中的执行

iphone - 如何从十六进制 NSString 获取十进制整数

ios App 在解除分配 UIView 子类实例时崩溃

ios - Xcode 调试 View 层次结构 : Unable to capture view hierarchy