0

Hey! Basically I wanna reverse geocode my coordinates for my annotation and show the address as the subtitle for my stored annotation (StorePosition). I just cant figure out how to do it...what am I doing wrong?

EDIT; .m

    -(NSString *)subtitle{          
 return subtitle;

    - (void)setCoordinate:(CLLocationCoordinate2D)c {
        MKReverseGeocoder *rvg = [[MKReverseGeocoder alloc] initWithCoordinate:c];
        rvg.delegate = self;
        [rvg start];
        coordinate = c;
    }

    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
        self.subtitle = [placemark.addressDictionary valueForKey:@"Street"]; 
    } 
Krismutt
  • 191
  • 2
  • 16

1 Answers1

3

//EDIT2: maybe that solves your problem, implement your own coordinate setter and request there your reverse geocoder:

-(NSString *)subtitle{
    if (!subtitle) {
        return @"No Street";
    } else {
        return subtitle;
    }
}

-(NSString *)title{
    return @"Saved position";
}

- (void)setCoordinate:(CLLocationCoordinate2D)c {
    MKReverseGeocoder *rvg = [[MKReverseGeocoder alloc] initWithCoordinate:c];
    rvg.delegate = self;
    [rvg start];
    coordinate = c;
}


-(id)initWithCoordinate:(CLLocationCoordinate2D) coor{
    self.coordinate=coor;
    NSLog(@"%f,%f",coor.latitude,coor.longitude);
    return self;
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
    NSLog(@"fail %@", error);
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
    self.subtitle = [placemark.addressDictionary valueForKey:@"Street"];
}

// ORIGINAL:
First lets talk about your bugs :D

return [NSString stringWithFormat:@"%f", streetAddress];

%f is for float values, so it wouldn't display a string.
The second thing, your variable streetAddress doesn't exist at this time you should make it accessible by writing it in your header file.

Then in initWithCoordinate: you call return self; all things after this line never gets called. So your ReverseGeocoder never start.

NSString *streetAddress = …

Here you want to save your address but you only write it into a variable which only exist in this method. After the method is done this variable is going to release. You would save it to your class wide variable if you had defined it in your header file.

Now let's talk about a way how you can do that. What I would do is to put the reverse geocoder out of your annotation and put it in your mapView controller. Then if you get a new location you change the subtitle of your annotation.

[[mapView.annotations objectAtIndex:0] setSubtitle:@"Address Information"];

//EDIT: you can log your didFailWith and you will see that he cannot request the position. Even if you put your reverseGeocoder-init above the return it still has not enough time to request the location.

  • Please have a look at the edit! :D I don't get how I'm supposed to do it for the StorePosition as well! – Krismutt Dec 29 '10 at 22:30
  • The self.coordinate=coor; wouldn't work if it's readonly so either you remove the self (but then the init location wouldn't be geocoded) OR you overwrite it with a readwrite in @interface above your @implementation (in your .m file) –  Dec 29 '10 at 22:51
  • I get two build errors: "'subtitle' undeclared (first use in this function)" and "object cannot be set - either readonly property or no setter found"... im a real newbie.. please helpz – Krismutt Dec 29 '10 at 22:52
  • in your header file: @property (nonatomic, copy) NSString *subtitle; @property (nonatomic, readwrite) CLLocationCoordinate2D coordinate; –  Dec 29 '10 at 22:54
  • Potential leak in: " MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:koor]; geocoder.delegate = self; coordinate = koor;" – Krismutt Dec 29 '10 at 23:09
  • put an autorelease] after the initWithCoordinate –  Dec 29 '10 at 23:11
  • create a retain-property for the geocoder, then you would write self.geocoder = ... and release it later (in dealloc) – Felix Dec 29 '10 at 23:19
  • That's better. I wanna save this subtitle when the application quits....do u know how i should implement that?? – Krismutt Dec 29 '10 at 23:47
  • come on, I can't write your complete App. You can save it using CoreData or a plist file to name only some of the many possibilities. Start reading the documentation or review some sample projects ;) –  Dec 30 '10 at 11:56