出於某種原因,我無法從我的代碼中獲取城市(地區)的名稱。請幫忙!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;
}
告訴我們更多關於您的問題!你期望看到什麼,你看到了什麼?給我們樣品拉特,長期價值,所以我們可以嘗試和幫助。 Google可以將Geocode逆轉爲您提供的緯度/經度? – Devraj
我得到了經緯度,沒有任何問題。當我嘗試使用該lat/long獲取城市的名稱時,我嘗試使用地標代碼,它在拋出錯誤「Request for member'locality'in something not a structure or union」 – Prajoth
@Prajoth:嘗試,只需在NSLog中打印placemark.locality。刪除其他語句。檢查是否顯示錯誤仍然存在。也可以嘗試在控制檯中打印地標。檢查發生了什麼 – Satya