ios - 如何在 Objective-C 中找到平均 10 个信号强度记录

标签 ios objective-c core-location cllocationmanager core-telephony

我试图找到 10 个信号强度值的平均值(以提高准确性,同时使用 CTGetSignalStrength)。我也在 try catch 用户位置(一次)。我目前有以下代码,但我意识到这行不通。如何安排 if 语句以确保记录 10 个信号强度值的平均值,同时在 buttonPressed 时只记录一个位置?

- (IBAction)buttonPressed:(id)sender {

    dispatch_async(backgroundQueue, ^{
        manager.delegate = self;
        manager.desiredAccuracy = kCLLocationAccuracyBest;
        [manager startUpdatingLocation];
    });

}

#pragma mark CLLocationManagerDelegate Methods

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Error: %@", error);
    NSLog(@"Failed to get location!");
}

- (void)arrayBuild {
    loopCount++;
    if (loopCount >= 11) {
        [myTimer invalidate];
        myTimer = nil;
        [manager startUpdatingLocation];

    } else {

    }

}

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


        NSLog(@"Location: %@",newLocation);
        CLLocation *curentLocation = newLocation;

        if (curentLocation != nil) {

        float signalstrength = CTGetSignalStrength();

        NSMutableArray *resultsArray = [[NSMutableArray alloc]init];
        NSNumber *avg = [resultsArray valueForKeyPath:@"@avg.self"];
        NSString *result = [NSString stringWithFormat:@"%f", signalstrength];
        NSInteger resultInt = [result integerValue];
        [resultsArray addObject:[NSNumber numberWithFloat:resultInt]];

        self.latitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
        self.longitude.text = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
        self.signal.text = [NSString stringWithFormat:@"%.8f",signalstrength];


        // Code below uses a third party utility to submit data

        PFObject *Object = [PFObject objectWithClassName:@"Issue"];

        Object[@"Latitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.latitude];
        Object[@"Longitude"] = [NSString stringWithFormat:@"%.8f",curentLocation.coordinate.longitude];
        Object[@"Signal"] = [NSString stringWithFormat:@"%@",avg];

        myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target:self
                                                 selector:@selector(arrayBuild)
                                                 userInfo:nil
                                                  repeats:YES];
        [Object saveInBackground];
    }
}

最佳答案

我会做什么:

1) 使用区域而不是一组坐标。要么解决传递数据的问题,要么不太优雅...为区域设置一个全局变量。

通过

注册以接收“区域更新”
startMonitoringForRegion:

然后每次用户四处移动时,使用对“区域更新”方法的委托(delegate)调用来更改全局变量。

2)委托(delegate)方法

-(void)locationManager: didUpdateToLocation: fromLocation:

...在 iOS 6.0 中已弃用 您应该为...设置一个委托(delegate)

- (void)locationManager: didUpdateLocations:

...然后调用

[CLLocationManager startUpdatingLocation];

您将立即调用您的委托(delegate)方法。

Xcode 中的文档说:

Specifically, it takes into account the values in the desiredAccuracy and distanceFilter property to determine when to deliver new events. The precision of the standard location services are needed by navigation applications or any application where high-precision location data or a regular stream of updates is required.

[咆哮]

(也许是您的意图,但我们在这里混合了两种不同的方案 - 标准位置委托(delegate)处理程序加上使用 CTGetSignalStrength 进行轮询...可能想要删除其中之一)

[/咆哮]

...所以我认为一旦你告诉父类(super class)你想要最好程度的位置调整,你只需要等待位置数据不断更新(即信号强度)。

3) 在 CTGetSignalStrength 上找不到很多定义信息,但我认为它返回一个 int,而不是一个 float。取平均值时要记住的一点是,您得到的是 - 分贝作为一个单位。由于它是对数的,因此您必须转换为十进制,即:

-65db, -40db, -75db 

可以...

10^(-65/10.), 10^(-40/10.), 10^(-75/10.)

在权力范围内。然后将这些值取平均值并转换回数据库。请仔细检查您在确切的模块/ header 中从 CTGetSignalStrength 返回的单位。希望对您有所帮助。

关于ios - 如何在 Objective-C 中找到平均 10 个信号强度记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25247206/

相关文章:

ios - 获取数组的数组内具有最大对象的数组

iphone - 在 iOS 中获取有关时区更改的通知

ios - 使用 NSTimer 更好地验证信标

IOS无限后台时间

ios - 在 UITextView 上以编程方式禁用 iOS8 Quicktype 键盘

ios - 当我拖动控件时,如何阻止 Interface Builder 在 UIView 中嵌入控件?

ios - dealloc 是否会调用属性的 setter 来消除它们?

iphone - 将蒙版图像作为 PNG 文件写入磁盘

objective-c - CGrect NSArray 错误

ios - 位置问题 swift 3