2011-09-05 29 views
1

出於某種原因,我無法從我的代碼中獲取城市(地區)的名稱。請幫忙!MKReverseGeocoder問題

- (void)viewDidLoad { 
     [super viewDidLoad]; 


     self.lm = [[CLLocationManager alloc] init]; 
     lm.delegate = self; 
     lm.desiredAccuracy = kCLLocationAccuracyBest; 
     lm.distanceFilter = kCLDistanceFilterNone; 
     [lm startUpdatingLocation]; 

    } 


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

     if (!geocoder) { 
      geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
      geocoder.delegate = self; 
      [geocoder start]; 
     } 

     NSString *lat = [[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude]; 
     NSString *lng = [[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude]; 
     NSString *acc = [[NSString alloc] initWithFormat:@"%f", newLocation.horizontalAccuracy]; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:lat message:lng delegate:self cancelButtonTitle:acc otherButtonTitles: @"button", nil]; 
     [alert show]; 
     [alert release]; 

     [lat, lng, acc release]; 


    } 

    - (void) reverseGeocoder:(MKReverseGeocoder *)geo didFailWithError:(NSError *)error { 
      [geocoder release]; 
      geocoder = nil; 

     } 



     - (void)reverseGeocoder:(MKReverseGeocoder *)geo didFindPlacemark:(MKPlacemark *)placemark { 

      **THIS IS WHERE THE ERROR IS OCCURRING** (REQUEST FOR MEMBER 'LOCALITY' IN SOMETHING NOT A STRUCTURE OR UNION) 

      location = [NSString stringWithFormat:@"%@", placemark.locality]; 
      [geocoder release]; 
      geocoder = nil; 
     } 
+1

告訴我們更多關於您的問題!你期望看到什麼,你看到了什麼?給我們樣品拉特,長期價值,所以我們可以嘗試和幫助。 Google可以將Geocode逆轉爲您提供的緯度/經度? – Devraj

+0

我得到了經緯度,沒有任何問題。當我嘗試使用該lat/long獲取城市的名稱時,我嘗試使用地標代碼,它在拋出錯誤「Request for member'locality'in something not a structure or union」 – Prajoth

+0

@Prajoth:嘗試,只需在NSLog中打印placemark.locality。刪除其他語句。檢查是否顯示錯誤仍然存​​在。也可以嘗試在控制檯中打印地標。檢查發生了什麼 – Satya

回答

0

我意識到,這是一個比較古老的問題,但...

我遇到了完全一樣的問題,並採取了使用addressDictionary標屬性,例如,

[placemark.addressDictionary [email protected]"City"] 

而不是

placemark.subAdministrativeArea 

我不明白爲什麼後者也不起作用。

0

當您嘗試訪問結構的成員時,通常會發生此錯誤,但在某些不是結構的情況下會發生此錯誤。例如:

struct { 
    int a; 
    int b; 
} foo; 
int fum; 
fum.d = 5; 

它也會發生如果您嘗試訪問實例時有一個指針,反之亦然。例如:

struct foo { 
    int x, y, z; 
}; 

struct foo a, *b = &a; 
b.x = 12; /* This will generate the error, it should be b->x or (*b).x */ 

它也出現,如果你這樣做:

struct foo { int x, int y, int z }foo; 
foo.x=12 

代替:

struct foo { int x; int y; int z; }foo; 
foo.x=12 

因爲那樣的話,你看起來像它在處理實例代碼,實際上它處理的是指針。

在我看來,你需要檢查你的代碼。 也許,試試這個:

NSString *strLocation = (NSString *)placemark.locality; // Get locality 
相關問題