涉及CLLocationManager的类实现

时间:2013-02-21 01:18:33

标签: ios objective-c cllocationmanager

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

请求帮助我

1 个答案:

答案 0 :(得分:2)

您创建了Localizacao个实例,然后拨打startLocalizacaostartLocalizacao方法设置位置管理器并要求它启动。然后记录Cidade 2。此日志在位置管理器完成初始化之前和应用程序收到第一个位置之前发生。所有这些初始化都由位置管理器在后台完成。

您没有做错任何事情(除了为尚未初始化的数据添加日志语句之外)。您只需要意识到位置事件是异步的,它们在可用时就会出现。