- #import <Foundation/Foundation.h>
- #import <MapKit/MapKit.h>
- #import "MyAnnotation.h"
- @interface MyAnnotationView : MKAnnotationView {
- UIImageView *calloutView;
- CGSize calloutViewSize;
- BOOL useCustomCalloutView;
- BOOL canShowCallout;
- }
- - (void)showCalloutView:(id)sender;
- - (void)hideCalloutView:(id)sender;
- - (BOOL) canShowCallout;
- - (void) setCanShowCallout: (BOOL) newValue;
- @property (nonatomic,retain) UIImageView *calloutView;
- @property (nonatomic) CGSize calloutViewSize;
- @property (nonatomic) BOOL useCustomCalloutView;
- @end
- #import "MyAnnotationView.h"
- @implementation MyAnnotationView
- @synthesize calloutView, calloutViewSize, useCustomCalloutView;
- - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
- if (self != nil) {
- self.image = [UIImage imageNamed:@"icon.png"];
- calloutView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width/2,self.bounds.origin.y,0,0)];
- calloutView.image = [[UIImage imageNamed:@"speech.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
- //default size - set as property annotationView.calloutViewSize = CGMake(blah, blah);
- calloutViewSize = CGSizeMake(200, 100);
- //set to no to start as defualt
- self.useCustomCalloutView = NO;
- [self addSubview:calloutView];
- }
- return self;
- }
- //override so use cannot set them
- - (BOOL) canShowCallout {
- return NO;
- }
- - (void) setCanShowCallout: (BOOL) newValue {
- canShowCallout = NO;
- }
- -(void)setSelected:(BOOL)_selected {
- if (self.useCustomCalloutView) {
- if (_selected) {
- [self showCalloutView:nil];
- } else {
- [self hideCalloutView:nil];
- }
- }
- [super setSelected:_selected];
- }
- - (void)showCalloutView:(id)sender {
- [UIView beginAnimations: nil context: nil];
- [UIView setAnimationDuration: 0.3];
- [UIView setAnimationCurve: UIViewAnimationCurveEaseIn];
- calloutView.frame = CGRectMake((self.bounds.size.width-calloutViewSize.width)/2 + self.calloutOffset.x,
- self.bounds.origin.y - calloutViewSize.height + self.calloutOffset.y,
- calloutViewSize.width,
- calloutViewSize.height);
- [UIView commitAnimations];
- }
- - (void)hideCalloutView:(id)sender {
- [UIView beginAnimations: nil context: nil];
- [UIView setAnimationDuration: 0.1];
- [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
- calloutView.frame = CGRectMake(self.bounds.size.width/2, self.bounds.origin.y,0,0);
- [UIView commitAnimations];
- }
- - (void)dealloc {
- [calloutView release];
- [super dealloc];
- }
- @end
- /*
- USAGE
- - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
- {
- static NSString *annotationViewID = @"PinID";
- MyAnnotation *myAnnotation = (MyAnnotation*) annotation;
- MyAnnotationView *annotationView =
- (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
- if (annotationView == nil)
- {
- annotationView = [[[MyAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:annotationViewID] autorelease];
- annotationView.canShowCallout = YES;
- annotationView.useCustomCalloutView = YES;
- }
- return annotationView;
- }