0

CLLocationManager と CLGeocoder を使用して iPhone ユーザーの場所を特定しようとしています。

私の CLLocationManager はうまく機能しているようですが、残念ながら CLGeocoder は私がやりたいことをしません。

一部のコードの抜粋:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    [self.locationManager stopUpdatingLocation];

    NSLog(@"Location updated to: %@",newLocation);

    [self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        NSLog(@"Reverse geocoding finished");

        self.currentPlacemark = [placemarks objectAtIndex:0];
        NSLog(@"%@",self.currentPlacemark.locality);
        NSLog(@"%@",self.currentPlacemark.ISOcountryCode); 

    }];
}

- (void)getCurrentLocation {

    NSLog(@"Determining current location");

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    [self.locationManager startUpdatingLocation];
}

- (void)viewDidLoad {

    [super viewDidLoad];
    [self getCurrentLocation];
}

self.currentPlacemarkCLPlacemarkオブジェクトです。

self.geocoderCLGeocoderオブジェクトです。

self.locationManagerCLLocationManagerオブジェクトです。

私は何が欠けていますか?なぜこれが機能しないのですか?私は正直に何時間も費やし、ゆっくりと自暴自棄になり始めています…</p>

4

1 に答える 1

3

はい、ジオコーダーを割り当てて初期化する必要があります。

self.geocoder = [[[CLGeocoder alloc] init] autorelease];
[self.geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    NSLog(@"Reverse geocoding finished");

    self.currentPlacemark = [placemarks objectAtIndex:0];
    NSLog(@"%@",self.currentPlacemark.locality);
    NSLog(@"%@",self.currentPlacemark.ISOcountryCode); 

}];
于 2012-06-08T11:24:15.783 に答える