views:

611

answers:

1

Hi,

I have a mapview with serveral annotations. Every annotation has a leftCalloutAccessoryView which is a UIViewController class. The reason for this is that I want every annotation to load some data from the server, and add the result of that data to the annotation subTitle. This all works perfectly, except that I dont want to load all that data when my app is started, but I want to the remote call to be done only when the callout bubble is opened.

Does anybody know how I can do this? The viewWillload, viewDidLoad ect. don't work in this case. Any examples as well?

Best regards, Paul Peelen

A: 

I solved the issue adding an observer. The observer then does its thing and after that the callout shows up.

Something I had problems with is that I couldn't update the information in the callout bubble after the bubble is shown. The only way to do this is to create your own callout bubble (as I understand it), which is something I didn't feel like given that I have a deadline. I fixed that by adding an extra UIView with an alpha on it and a text "Getting location data...". I just show up that view when pressing an location and when the observer is done, I hide the view again (off course by using an animation).

Hope my answer helped others.

Code:

[pin addObserver:self
      forKeyPath:@"selected"
         options:NSKeyValueObservingOptionNew
         context:GMAP_ANNOTATION_SELECTED];

Some more code:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;

    MKAnnotationView *annotationView = [(MKAnnotationView*)object retain];
    BikeAnnotation *bike = [[annotationView annotation] retain];

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED] && [[bike _stationType] intValue] != 5 && [[bike _stationType] intValue] != 6){
        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            NSLog(@"Annotation selected");
        else {
            NSLog(@"annotation deselected");
        }
    }
}

And put this just after the @synthesize's:

NSString * const GMAP_ANNOTATION_SELECTED = @"gmapselected";
Paul Peelen