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.
Asked
Active
Viewed 99 times
0
-
Do you mean the subtitle would say something like "12 minutes ago"? In your annotation class, do you store the date/time the "something" happened? What have you tried? – May 18 '12 at 15:27
-
@Anna Karenina Yes, exactly something like "12 minutes ago". I do store date in my annotation class. I am thinking about concept now - seems that i can choose observers, notification or Martin's concept (below), however i wish to make it this way that only if pin currently has visible callout then it's refreshed. – Nat May 25 '12 at 11:44
1 Answers
0
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.

Martin
- 11,881
- 6
- 64
- 110
-
I have 2 own annotation classes for each of these 2 types of pins. Anyway, i don't like idea to run this loop each second, it seems unnecessary(i have a LOT of pins, loop goes through all- not only the visible ones or ones with callout). Is there any way to to send this update only to pin that is currently shown with its callout? If not, then your solution or observers seems to be proper solution (both). – Nat May 25 '12 at 11:40
-
Okay, you're right, in your case refreshing each n second is a pretty bad solution... See my edits for another idea. – Martin May 25 '12 at 12:15
-
Thanks, will try it when i get back to this topic and accept answer if its ok :) – Nat May 25 '12 at 16:23
-
Hey, I'm curious if it worked for you. Have you tried since last update ? – Martin Jun 05 '12 at 13:14
-