Advertisement
Guest User

MKAnnotation title

a guest
Oct 16th, 2012
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @interface MyAnnotationClass : NSObject<MKAnnotation>
  2.  
  3. @property (nonatomic, retain) NSString *title;
  4. @property (nonatomic, retain) NSString *subtitle;
  5. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
  6.  
  7. -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
  8.  
  9. @end
  10.  
  11.  
  12. MyAnnotationClass.m
  13.  
  14. #import "MyAnnotationClass.h"
  15.  
  16. @implementation MyAnnotationClass
  17.  
  18. -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{
  19.     self=[super init];
  20.     if(self){
  21.         _coordinate = coordinate;
  22.     }
  23.     return self;
  24. }
  25.  
  26. -(void) dealloc{
  27.     [_title release];
  28.     [_subtitle release];
  29.     [super dealloc];
  30. }
  31. @end
  32.  
  33.  
  34. ViewController.h
  35.  
  36. #import <UIKit/UIKit.h>
  37. #import <MapKit/MapKit.h>
  38.  
  39. @interface ViewController : UIViewController<MKMapViewDelegate> {
  40.     IBOutlet MKMapView *_myMapView;
  41.     NSArray *_myAnnotations;
  42. }
  43.  
  44. @property (nonatomic, retain) NSArray *myAnnotations;
  45. @property (nonatomic, retain) IBOutlet MKMapView *myMapView;
  46.  
  47. @end
  48.  
  49.  
  50. ViewController.m
  51.  
  52. #import "ViewController.h"
  53. #import "AppDelegate.h"
  54. #import <MapKit/MapKit.h>
  55. #import <CoreLocation/CoreLocation.h>
  56. #import "PlaceMark.h"  
  57. #import "MyAnnotationClass.h"
  58. @interface ViewController ()
  59.  
  60. @end
  61.  
  62. @implementation ViewController
  63. @synthesize myMapView = _myMapView;
  64. @synthesize myAnnotations = _myAnnotations;
  65.  
  66. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  67. {
  68.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  69.     if (self) {
  70.         // Custom initialization
  71.     }
  72.     return self;
  73. }
  74.  
  75. -(void) viewDidLoad{
  76.     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
  77.    
  78.     //Initialize annotation
  79.     MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
  80.     commuterLotAnnotation.title = @"Hello title";
  81.     commuterLotAnnotation.subtitle = @"Correct";
  82.  
  83.     MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
  84.     overflowLotAnnotation.title = @"Hello title";
  85.     overflowLotAnnotation.subtitle = @"Correct";
  86.  
  87.     //Add them to array
  88.     self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];
  89.    
  90.     //Release the annotations now that they've been added to the array
  91.     [commuterLotAnnotation release];
  92.     [overflowLotAnnotation release];
  93.    
  94.     //add array of annotations to map
  95.     [_myMapView addAnnotations:_myAnnotations];
  96. }
  97.  
  98.  
  99. -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
  100.     static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";
  101.    
  102.     if([annotation isKindOfClass:[MyAnnotationClass class]]){
  103.         //Try to get an unused annotation, similar to uitableviewcells
  104.         MKAnnotationView *annotationView=[_myMapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
  105.  
  106.         //If one isn't available, create a new one
  107.         if(!annotationView){
  108.             annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
  109.             //Here's where the magic happens
  110.             annotationView.image=[UIImage imageNamed:@"apple.gif"];
  111.             annotationView.canShowCallout = YES;
  112.         }
  113.         return annotationView;
  114.     }
  115.     return nil;
  116. }
  117.  
  118.  
  119. - (void)viewDidUnload
  120. {
  121.     [super viewDidUnload];
  122.     // Release any retained subviews of the main view.
  123. }
  124.  
  125. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  126. {
  127.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  128. }
  129.  
  130.  
  131. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement