【问题标题】:Pass Variable to CLLocationManager in Objective-C在 Objective-C 中将变量传递给 CLLocationManager
【发布时间】:2023-12-22 14:51:02
【问题描述】:

如何将avg 的实例传递给:

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

下面的功能?我想取数组的平均值,然后在位置更新时将其提交到外部数据库。

- (IBAction)buttonPressed:(id)sender {

    // Make Manager Delegate & Set Accuracy
    manager.delegate = self;
    manager.desiredAccuracy = kCLLocationAccuracyBest;

    // Call Timer
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                               target:self
                                             selector:@selector(arrayBuild)
                                             userInfo:nil
                                              repeats:YES];
    // Initialise Array
    resultsArray = [[NSMutableArray alloc]init];
}

- (void)arrayBuild {

    loopCount++;

    if (loopCount >= 11) {

        // Find Average
        NSNumber *avg = [resultsArray valueForKeyPath:@"@avg.self"];

        // Change Text of Label to Average & Log
        self.signal.text = [NSString stringWithFormat:@"%@",avg];
        NSLog(@"%@",avg);
        // I want to send this value here ^^ to the didUpdateLocation function

        // Update Location
        [manager startUpdatingLocation];

        // Invalidate Timer
        [myTimer invalidate];
        myTimer = nil;

    }else{

        // Declare Signal Strength
        float signalstrength = CTGetSignalStrength();

        // Individual Result & Convert to Integer
        NSString *result = [NSString stringWithFormat:@"%f", signalstrength];
        NSInteger resultInt = [result integerValue];

        // Add Object
        [resultsArray addObject:[NSNumber numberWithFloat:resultInt]];

        // Change Text of Label Each Second
        self.signal.text = [NSString stringWithFormat:@"%d",loopCount];
        NSLog(@"%f",signalstrength);
    }
}

#pragma mark CLLocationManagerDelegate Methods

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {

    NSLog(@"Error: %@", error);
    NSLog(@"Failed to get location!");

}

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

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

    if (curentLocation != nil) {

        float signalstrength = CTGetSignalStrength();

        // 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:@"%f",signalstrength];
        [Object saveInBackground];

        // Update Text Fields
        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];
    }
    // Stop the Location Update
    [manager stopUpdatingLocation];
}

我认为它会涉及 [self manager:avg] 或类似的东西?但我不确定到底是什么。

【问题讨论】:

    标签: ios objective-c cllocationmanager


    【解决方案1】:

    对平均变量使用实例变量(或属性),这样您就可以在 arrayBuild 函数中计算和设置它,并在 didUpdateLocation 函数中访问它。

    【讨论】: