出于某种原因,我无法从我的代码中获取城市名称(地区)。请帮忙!
- (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;
}
答案 0 :(得分:0)
我意识到这是一个相对古老的问题,但是......
我遇到了完全相同的问题并使用了addressDictionary地标属性,例如,
[placemark.addressDictionary objectForKey@"City"]
而不是
placemark.subAdministrativeArea
我不明白为什么后者也不起作用。
答案 1 :(得分: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