我在实现类位置时遇到了一些问题。当我启动应用程序时,我在 StartApp.m 中调用类 Location,我想打印以测试变量 cidade,但 Cidade2 和 Cidade3 的结果在 NSLog 中为空,我做错了什么?我尝试更改代码,但无法解决问题。我不明白的另一件事是为什么 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
启动应用程序.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
谢谢帮助我