Advertisement
Guest User

MKAnnotation

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