views:

511

answers:

2

I want to update pin callout (popup) subtitle either realtime, when I receive some new info from server, or at least when callout opens. So far it looks like pin + callout are created only once at...

- (MKAnnotationView *)mapView:(MKMapView *)aMapView 
        viewForAnnotation:(id <MKAnnotation>)annotation

...and then used as-is as long as it exists regardless how many times I tap it closed/open or scroll around keeping it visible. How can I update the subtitle?

A: 

Hi,

I can't answer completely beacuse I've never needed to do it but this bit of documentation looks like a pointer in the right direction.

Try just updating the subititle property of your <MKAnnotation> object when a new response comes in from the server and the MapView should update itself?

If not, try removing and re-adding the annotation when the info changes?

deanWombourne
Pins are for search results, so there's a lot of them. Update from server just gives some real-time info for those results. Don't know (yet) how to identify one from another... but that tip about KVO looks promising!
JOM
Looks like you're going to have to store which MKAnnotation belongs to which results from the server - some sort of NSDictionary perhaps? Let me know how you get on - I'm really interested in how you solve this!
deanWombourne
Thanx, you set me to right direction :) Not completely solved yet, but works somehow, when I always update subtitle at viewForAnnotation. This sample code was helpful http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html
JOM
A: 

edit:: There's a much better solution using key-value observing, which includes adding an observer to the selected property of the MKAnnotationView, then dealing with it appropriately in the callback:

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

You can view the solution here: http://blog.evandavey.com/2009/07/how-to-detect-when-mkannotation-mkannotationview-is-selected.html


(old solution)

I had a similar problem. It looks like you've gotten most of it. In your function:

- (MKAnnotationView *)mapView:(MKMapView *)aMapView 
    viewForAnnotation:(id <MKAnnotation>)annotation

Just make sure to add a callback (aka. selector) to each annotation. For me, I actually want a UIButton for them to click on, so I created one and added it as the rightCalloutAccessoryView for the annotation.

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        customPinView.rightCalloutAccessoryView = rightButton;

I then created a function called showDetails in whatever delegate you passed to the addTarget variable (in my case self, which is a custom UIViewController).

Now, whenever anyone clicks on any annotation, the showDetails function gets called.

However, I'm having problems with figuring out which specific annotation gets called, as I have many on my map, and the caller of the showDetails function is not the annotation, but the specific UIButton.

I've looked into this solution: http://stackoverflow.com/questions/2329654/clean-solution-to-know-which-mkannotation-has-been-tapped, but it only deals with the event inside viewForAnnotation, which, as you mentioned, only gets called at the creation of each annotation, and not when they get tapped.

Jon