1

For some reason, I cannot get the name of the city (locality) from my code. Please help!

 - (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;
        }
Prajoth
  • 900
  • 3
  • 12
  • 26
  • 1
    Tell us more about your problem! What do you expect to see, what do you see instead? Give us sample lat, long values so we can try and help. Can Google reverse Geocode the lat/lng that you've provided? – Devraj Sep 05 '11 at 05:20
  • I am getting the lat and long accurately without any problem. When I try to get the name of the city using that lat/long, I am trying to use the placemark code, which is throwing up an error "Request for member 'locality' in something not a structure or union" – Prajoth Sep 05 '11 at 05:29
  • @Prajoth:Try, just printing the placemark.locality in the NSLog. remove the other statements. Check is it showing the error still. Also try printing the placemark in the Console. Check what happened – Satya Sep 05 '11 at 07:17
  • I tried both of them, and neither of them are working. Xcode is just not recognizing placemark.locality =[ – Prajoth Sep 05 '11 at 20:41

2 Answers2

0

This error usually occurs when you are trying to access a member of a structure, but in something that is not a structure. For example:

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

It also happens if you're trying to access an instance when you have a pointer, and vice versa. For example:

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 */

It also appears if you do this:

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

instead of:

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

Because then, you get a code that looks like it's dealing with instances, when in fact it's dealing with pointers.

It seems to me that you need to check on your code. Perhaps, try this:

NSString *strLocation = (NSString *)placemark.locality; // Get locality
Kimpoy
  • 1,974
  • 3
  • 21
  • 38
0

I realize that this is a relatively old question, but ...

I ran into the exact same issue and resorted to using the addressDictionary placemark attribute, e.g.,

[placemark.addressDictionary objectForKey@"City"]

instead of

placemark.subAdministrativeArea

I don't understand why the latter doesn't work either.

larick
  • 259
  • 5
  • 12