Reputation: 992
I have two global NSString variables called myLocality and myCountry...
I have set GeoCode up as below:
- (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;
}
}
And my background thread is as follows:
- (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
});
}
(Yeah, I know it's pretty sloppy code, but, meh.. xD)
Now, what I can't figure out is why these are returning null when I log them in my code elsewhere.
However, when I put log in the above for
, i.e NSLog(@"%@", placemark.locality)
I get the correct value, or at least I get a value.
How can I set the global variables from the placemark
?
Upvotes: 0
Views: 185
Reputation: 992
OK, I found out my problem. Turns out, I was accessing the variables too early. I simply changed the do while
loop's condition to myCountry == null
and it worked =]. Thanks for the help.
Upvotes: 0
Reputation: 628
Create locality and country properties in AppDelegate and access them where ever you want.
// 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;
You can also create a dedicated singleton class and use it instead of using app delegate.
Upvotes: 1
Reputation: 1714
Where are these variables declared?
If you make myLocality
and myCountry
static in your class that is doing the geocoding, then other classes can access them once they are set.
Another way to do it would be to create a @protocol
and have the class that wants the results of the geocoding to receive a callback once it's done.
Upvotes: 1