0

我有两个名为 myLocality 和 myCountry 的全局 NSString 变量...

我已将 GeoCode 设置如下:

    - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    finishedGettingLocation = false;
    if (newLocation.horizontalAccuracy < 200.0f){
        [locationManager stopUpdatingLocation];
        CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
        __block NSString *locality;
        __block NSString *country;
        [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            for (CLPlacemark * placemark in placemarks) {
                locality = placemark.locality;
                country = placemark.ISOcountryCode;
            }
        }];
        do {

        } while ([geoCoder isGeocoding]);

        myLocality = locality;
        myCountry = country;

        finishedGettingLocation = true;        
    }    
}

而我的后台线程如下:

- (void) doWork
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{                   

        finishedGettingLocation = false;

        dispatch_async(dispatch_get_main_queue(), ^{
            locationManager = [[CLLocationManager alloc] init];
            [locationManager setDelegate:self];

            [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
            [locationManager startUpdatingLocation];
        });

        int counter = 0;
        do {
            [NSThread sleepForTimeInterval:.1];
            counter ++;
            if (counter % 200 == 0){
                dispatch_async(dispatch_get_main_queue(), ^{
                    Toast *oToast = [Toast toastWithMessage:@"Gps pin-pointing works faster if you are outside"];
                    [oToast showOnView:self.view];
                });
                counter = 0;
            }

        } while (!finishedGettingLocation);

      NSLog(@"%@", myCountry); // this is where I am trying to print
      NSLog(@"%@", myLocality); // this is where I am trying to print

    });
}

(是的,我知道这是相当草率的代码,但是,嗯.. xD)

现在,我无法弄清楚为什么当我将它们记录到其他地方的代码中时它们返回 null 。但是,当我在上面输入 log 时for,即NSLog(@"%@", placemark.locality)我得到了正确的值,或者至少我得到了一个值。

如何设置全局变量placemark

4

3 回答 3

1

这些变量在哪里声明?

如果您在进行地理编码的类中创建myLocalitymyCountry静态,则其他类一旦设置就可以访问它们。

另一种方法是创建一个@protocol并让希望地理编码结果在完成后接收回调的类。

于 2013-10-06T23:52:07.760 回答
1

在 AppDelegate 中创建位置和国家属性,并在您想要的任何地方访问它们。

// Set values
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.locality = @"Some Area";
appDelegate.country = @"Some Country";


// Get values
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

NSString *myLocality = appDelegate.locality;
NSString *myCountry = appDelegate.country;

您还可以创建一个专用的单例类并使用它而不是使用应用程序委托。

于 2013-10-07T01:45:17.937 回答
0

好的,我发现了我的问题。事实证明,我太早地访问了变量。我只是将do while循环的条件更改为myCountry == null并且它起作用了=]。谢谢您的帮助。

于 2013-10-07T15:53:44.860 回答