クラス Location の実装に問題があります。StartApp.m で Location クラスを呼び出すアプリケーションを起動すると、印刷して変数 cidade をテストしたいのですが、Cidade2 と Cidade3 の結果が NSLog で null になっています。間違ったことをしていますか? コードを変更しようとしましたが、問題を解決できません。私が理解していない他のことは、NSLog Cidade2 が nil を出力している理由です。変数は Location クラスでもあります。助けてくれてありがとう!
出力:
2013-02-20 21:58:09.648 MPA[484:11603] Cidade 2: (null)
2013-02-20 21:58:09.650 MPA[484:11603] Cidade 3 : (null)
2013-02-20 21:58:09.661 MPA[484:11603] Resolving the Address
2013-02-20 21:58:10.390 MPA[484:11603] Found placemarks: (
"Apple Store, San Francisco, Apple Store, San Francisco, 1800 Ellis St, San Francisco, CA 94115-4004, United States @ <+37.78584540,-122.40651750> +/- 100.00m, region (identifier <+37.78584545,-122.40652160> radius 18.96) <+37.78584545,-122.40652160> radius 18.96m"
), error: (null)
2013-02-20 21:58:10.391 MPA[484:11603] Cidade : California
Localizacao.m
#import "Localizacao.h"
@interface Localizacao ()
@end
@implementation Localizacao
@synthesize cidade,estado,latSend,lonSend;
-(void)startLocalizacao
{
lm = [[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
lm.distanceFilter = kCLDistanceFilterNone;
[lm startUpdatingLocation];
geocoder = [[CLGeocoder alloc]init];
NSLog(@"Cidade 2: %@", cidade);
}
//Adicionado para pegar a localizacao atual
-(void) locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//Display Latitude
latSend = newLocation.coordinate.latitude;
//Display Longitude
lonSend = newLocation.coordinate.longitude ;
//Display Accurancy
NSString *acc = [[ NSString alloc ] initWithFormat:@"%f",
newLocation.horizontalAccuracy];
//Update MAP
MKCoordinateSpan span;
span.latitudeDelta = .001;
span.longitudeDelta= .001;
MKCoordinateRegion region;
region.center = newLocation.coordinate;
region.span = span;
[acc release];
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
/*NSString *teste = [[NSString alloc ] initWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare,
placemark.thoroughfare,
placemark.postalCode,
placemark.locality,
placemark.administrativeArea,
placemark.country];*/
cidade = placemark.administrativeArea;
estado = placemark.locality;
[lm stopUpdatingLocation];
NSLog(@"Cidade : %@", cidade);
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
@end
StartApp.m
#import "StartApp.h"
#import "Localizacao.h"
@interface StartApp ()
@end
@implementation StartApp
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Localizacao *localizacao = [[Localizacao alloc] init];
[localizacao startLocalizacao];
NSLog(@"Cidade3 : %@", localizacao.cidade);
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
助けてくれてありがとう