2012-11-05 38 views
1

我正在使用此代碼獲取地標,但未給出城市名稱。 早些時候我使用MKReverse地理編碼器來獲取我正在獲取城市名稱的地標,但在iOS 6中它顯示不推薦使用,因爲Apple開發人員在CLLocation中添加了所有內容。iOS 6中未給出城市名稱的地標6

所以我用這個代碼:

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

    CLLocation *location = [locationManager location]; 
    NSLog(@"location is %@",location); 

    CLGeocoder *fgeo = [[[CLGeocoder alloc] init] autorelease]; 

    // Reverse Geocode a CLLocation to a CLPlacemark 
    [fgeo reverseGeocodeLocation:location 
      completionHandler:^(NSArray *placemarks, NSError *error){ 

       // Make sure the geocoder did not produce an error 
       // before continuing 
       if(!error){ 
        // Iterate through all of the placemarks returned 
        // and output them to the console 
        for(CLPlacemark *placemark in placemarks){ 
          NSLog(@"%@",[placemark description]); 
          city1= [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey]; 
          NSLog(@"city is %@",city1); 
        } 
       } 
       else{ 
        // Our geocoder had an error, output a message 
        // to the console 
        NSLog(@"There was a reverse geocoding error\n%@", 
             [error localizedDescription]); 
       } 
     } 
    ]; 

}

這裏,因爲我在控制檯我看到在NSLog(@"%@",[placemark description]); 它給像輸出: - ABC路名abc路名,國家名,國家名稱。

回答

4

如果你想的NSLog你來撰寫它做這樣的事情地址:

NSString *street = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStreetKey]; 
NSString *city = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCityKey]; 
NSString *state = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressStateKey]; 
NSString *country = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressCountryKey]; 
NSString *zip = [[placemark addressDictionary] objectForKey:(NSString *)kABPersonAddressZIPKey]; 

NSString *message = [NSString stringWithFormat:@"Address Is: %@, %@ %@, %@, %@", street, zip, city, state, country]; 

NSLog(@"%@", message); 

或者您也可以通過數組迭代通過返回:

NSArray *array = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"]; 

如果你想簡單打印字典內容,做:

NSLog(@"%@", [[placemark addressDictionary] description]); 
+0

對不起,這個答案不是那麼清楚的結果,因爲我一直在想你的問題是試圖登錄所有的字典,只有現在我閱讀了帖子標題中的真實問題......你是否使用FormattedAddressLines看到了這個城市?你的代碼看起來很好,你有沒有嘗試過另一個位置? – LombaX

4

我看到的三件事可以使用一些ttention ...

首先,這種方法已被棄用(see documentation here):

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

嘗試使用這個:

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 

您最當前的位置是數組locations所以在最後一個項目:

/* 
According to the Apple docs, the array of locations keeps the most recent location as the 
last object in the array 
*/ 
CLLocation *location = [locations lastObject]; 
NSLog(@"%@", location); 

其次,如果你使用ARC,a utorelease消息是不必要的:CLGeocoder *fgeo = [[CLGeocoder alloc] init];工作正常。

最後,根據蘋果CLPlacemark的文檔,有一個屬性locality應該返回城市的地標。所以

for(CLPlacemark *placemark in placemarks){ 
    NSLog(@"%@",[placemark description]); 
    NSString *city1 = [placemark locality]; 
    NSLog(@"city is %@",city1); } 

如果你只是想要的城市,addressDictionary屬性似乎是矯枉過正。根據documentation here addressDictionary被格式化爲返回ABPerson對象中的東西,我假設您必須解析該對象以獲取城市。標地點似乎要簡單得多......

我測試了我的建議,在我建立一個地理編碼應用程序,我得到了你正在尋找通過[placemark locality]

+0

嘿你的答案看起來很有用,但它不能在我的設備上工作。我在這裏檢查它也給我同樣的東西。 – Sawant

+0

謝謝,很高興幫助...嗯...(遠程)可能您的地標位於沒有地點的位置。 IOW,一個未註冊的地點。我住在美國佛羅里達州南部,我能夠測試代碼,並在測試時返回設備所在的城市(Hallandale Beach,FL)。你能改變位置並再次測試嗎? –

相關問題