2012-06-08 93 views
0

我試圖確定使用CLLocationManager和CLGeocoder了iPhone用戶的位置。CLGeocoder不返回任何

我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對象。

我錯過了什麼?爲什麼這不起作用? 老實說,我花了幾個小時,我慢慢開始變得絕望......

+0

你能更具體地說「不做我想做的事」嗎?特別是,它在做什麼?還是不行? – gaige

+0

當然,抱歉在這一點不清楚:completionHandler塊永遠不會被輸入(或換句話說:我從來沒有看到日誌語句「反向地理編碼已完成」) – Patrick

+1

你有alloc和初始化你的地理編碼器嗎? –

回答

3

是的,你應該alloc和初始化你的地理編碼器。

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); 

}];