Question

I have map with multiple pins (2 types of pins). I them to have timer as subtitle (how much time passed since something). The problem is that i'm not sure how to do it. Surely timer should be placed on viewController with mapView. Then probably some observer settled for annotations... But in this concept where to remove observer and where to set action for observers changes..? Or maybe there is any better concept? Thanks for any reply.

Was it helpful?

Solution

I would make my own MKAnnotation Class.

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D _coordinate;
    NSMutableString * title;
    NSMutableString * subtitle;
    ...
    UIImage * pinImage;
    ...
    NSDate * startTime;
}

This class would have a NSDate * startTime as property and a refreshTime method, which compare the current time ([NSDate date]) and display in subtitle the time elapsed.

Then you just have to refresh your maps annotations

for(MyAnnotation * annotation in [mapView annotations]) {
    if([annotation isKindOfClass:[MyAnnotation class]]) // important cause the user centered blue point is part of your annotation list!
        [annotation refreshTime];
}

Now the question is : when do you want to refresh ?

You can use a timer that refreshs every n seconds, for example, or launch the refresh on ViewDidAppear... it depends on what you want to do !

Edit :

If you want to refresh on annotation selection, maybe you can create your own AnnotationView subclass of MKAnnotationView.

Then in your AnnotationView, override the touchesBegan:withEvent: or touchesEnded:withEvent: to send the update.

I have not tested but I'm pretty sure that it would work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top