2012-12-05 42 views
1

我與locationManager'sdidUpdateToLocation委託但我想知道的一些價值越來越空,而我在Device測試應用程序中使用MKReverseGeocoderiphone: - MKReverseGeocoder獲取POSTALCODE,subAdministrativeArea,局部性的空值和subThoroughfare

我的代碼是貝婁: -

- (void)viewDidLoad 
{ 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    [locationManager startUpdatingLocation]; 


    [super viewDidLoad]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates 
    reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
    reverseGeocoder.delegate = self; 
    [reverseGeocoder start]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 
{ 
    NSLog(@"MKReverseGeocoder has failed."); 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    MKPlacemark * myPlacemark = placemark; 
    NSString *city = myPlacemark.thoroughfare; 
    NSString *subThrough=myPlacemark.subThoroughfare; 
    NSString *locality=myPlacemark.locality; 
    NSString *subLocality=myPlacemark.subLocality; 
    NSString *adminisArea=myPlacemark.administrativeArea; 
    NSString *subAdminArea=myPlacemark.subAdministrativeArea; 
    NSString *postalCode=myPlacemark.postalCode; 
    NSString *country=myPlacemark.country; 
    NSString *countryCode=myPlacemark.countryCode; 


    NSLog(@"city%@",city); 
    NSLog(@"subThrough%@",subThrough); 
    NSLog(@"locality%@",locality); 
    NSLog(@"subLocality%@",subLocality); 
    NSLog(@"adminisArea%@",adminisArea); 
    NSLog(@"subAdminArea%@",subAdminArea); 
    NSLog(@"postalCode%@",postalCode); 
    NSLog(@"country%@",country); 
    NSLog(@"countryCode%@",countryCode); 

} 

輸出

cityDrive-in Road

subThrough(null)

locality(null)

subLocality(null)

adminisAreaGujarat

subAdminArea(null)

postalCode(null)

countryIndia

countryCodeIN

USING CLGeocoder

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates 
// reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
// reverseGeocoder.delegate = self; 
// [reverseGeocoder start]; 

    CLGeocoder *reverseGeo = [[CLGeocoder alloc] init]; 

    [reverseGeo reverseGeocodeLocation: newLocation completionHandler: 
    ^(NSArray *placemarks, NSError *error) { 

     NSLog(@"%@",[placemarks objectAtIndex:0]); 
     for (CLPlacemark *placemark in placemarks) { 



      NSLog(@"~~~~~~~~%@",placemark.locality); 

     } 


    }]; 
} 

OUTPUT IS:- ~~~~~~~~(null)

我上面的代碼在設備中正確的模擬器,但沒有工作.. :(請幫助和指導我正確的方式來獲得城市名稱

+0

你是否能解決這個問題?我似乎得到了街道,城市,國家,除了「亞行」......爲什麼會發生這種情況?它是否與特定國家做任何事情?我來自印度。 BTW .. – GenieWanted

+0

這是不確定每次我們得到的結果MKReverseGeocoder,因爲它已被iOS5的蘋果貶值,所以我們neet使用其替代。 –

+0

那麼,替代解決方案是什麼? BTW。我正在使用CLGeocoder對象來進行反向地理編碼,但似乎並沒有顯示某些信息,如postalCode,subthoroughFare(門號)。對此有何想法? – GenieWanted

回答

2

你真的不應該使用MKReverseGeocoder作爲它已被iOS5中的蘋果折舊,你應該真的使用CLGeocoder。 CLGeocode將返回基於NSArray *地標的信息,然後您可以遍歷它們並調用assoc數組中的鍵。 請通過鏈接。

CLGeoCode Class Reference

Related sample code

+0

我也在使用CLGeocode,但仍然同樣的概率看到我更新的問題 –

+0

我在我的末端使用了您的更新代碼,它對我來說工作正常。請確保您在newLocation中獲取更新位置(經度,緯度)。 –

+0

兄弟在我的iPhone操作系統io6和我工作在xcode4.5.1其運行良好在模擬器概率只在設備.. :( –