Search code examples
iphoneaddressbookcllocationmanagermkannotationviewmkreversegeocoder

Setting street address as the subtitle in an annotation


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"]; 
    } 

Solution

  • //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.